Titulek je lehce zavádějící, jelikož Delphi má OOP přístup k souborům odjakživa, ale nyní se jedná ještě o jiný přístup. Raději to ukáži na příkladu.
V jednotce jsou definovány tyto třídy (resp. jsou to spíše nové recordy - jak již víme tak Delphi od D2007 - nebo tak nějak - umožňují definovat u záznamů metody):
Tento objektový přístup je podobný jako v .NET.
Tento record obsahuje metody na práci s adresáři, jeho hledání, získávání seznamu souborů, vytváření, mazání a nebo třeba informace o posledním přístupu, celkem odhadem cca 120 metod.
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, IOUtils;
var
sPath : string;
begin
for sPath in TDirectory.GetFiles('c:\temp', '*.*') do
writeln(Format('Name = %s', [sPath]));
end.
nebo třeba
if not TDirectory.Exists(edtPath.Text) then
blabla
To bylo jednoduché. Mnohem zajímavější je následující příklad.
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, IOUtils;
var
sPath : string;
FilterPredicate : TDirectory.TFilterPredicate;
begin
FilterPredicate :=
function(const Path: string; const SearchRec: TSearchRec): Boolean
begin
Result := (TPath.MatchesPattern(SearchRec.Name, 'b*.*', False));
end;
for sPath in TDirectory.GetFiles('c:\temp', FilterPredicate) do
writeln(Format('Name = %s', [sPath]));
end.
Definujeme anonymní metodu FilterPredicate a tu předhodíme do GetFiles, která vrátí TStringDynArray, které projdeme.
Kromě jiného jsou definovány třeba následující metody:
TDirectory = record
class function IsEmpty(const Path: string): Boolean; static;
class procedure Copy(const SourceDirName, DestDirName: string); static;
class procedure CreateDirectory(Path: string); static;
class procedure Delete(const Path: string; const Recursive: Boolean); overload; static;
class function Exists(const Path: string): Boolean; inline; static;
class function GetAttributes(const Path: string): TFileAttributes; inline; static;
class function GetCurrentDirectory: string; inline; static;
…
nebo již použité MatchesPattern
end;
Obsahuje metody pro práci s cestou, tj. například:
class function GetExtension(const FileName: string): string; static;
class function GetFileName(const FileName: string): string; inline; static;
class function GetFileNameWithoutExtension(const FileName: string): string; static;
class function GetFullPath(const Path: string): string; static;
class function GetInvalidFileNameChars: TCharArray; inline; static;
class function GetInvalidPathChars: TCharArray; inline; static;
class function GetPathRoot(const Path: string): string; static;
class function GetRandomFileName: string; static;
class function GetTempFileName: string; static;
class function GetTempPath: string; static;
A poslední pro práci se souborem jako načtení, vytvoření, nastavení atributů atd. Velmi užitečné.
TFile.Copy(source, dest);
TFile.Move(source, dest);
source := 'c:\temp\t1.txt';
dest := 'c:\temp\t2.txt';
bak := 'c:\temp\t3.txt';
TFile.Replace(source, dest, bak);
var
path: string;
sw: TStreamWriter;
begin
path := 'c:\temp\test.txt';
sw := TFile.CreateText(path);
sw.Write(123);
sw.Write('ABC');
sw.Close;
end;
var
path: string;
begin
path := 'c:\temp\test.txt';
TFile.AppendAllText(path, 'NewString', TEncoding.UTF8);
end;
TFile.WriteAllText(path, '123', TEncoding.UTF8);
str := TFile.ReadAllText(path, TEncoding.UTF8);
var
arr: TStringDynArray;
…
arr := TFile.ReadAllLines(path);
arr := TFile.ReadAllLines(path, TEncoding.UTF8);
TFile.Encrypt();
TFile.Decrypt();
a další. Speciálně upozorňuji na TEncoding, tím se dá specifikovat kódování při práci se soubory.
Datum: 2010-01-13 20:59:00 Tagy: Delphi 2010, RTL