vložil Radek Červinka
13. ledna 2010 21:59
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.
TDirectory
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.
1program Project1;
2
3uses
4 SysUtils, IOUtils;
5var
6 sPath : string;
7begin
8 for sPath in TDirectory.GetFiles('c:\temp', '*.*') do
9 writeln(Format('Name = %s', [sPath]));
10end.
11
12nebo třeba
13 if not TDirectory.Exists(edtPath.Text) then
14 blabla
To bylo jednoduché. Mnohem zajímavější je následující příklad.
1program Project1;
2
3uses
4 SysUtils, IOUtils;
5var
6 sPath : string;
7 FilterPredicate : TDirectory.TFilterPredicate;
8
9begin
10 FilterPredicate :=
11 function(const Path: string; const SearchRec: TSearchRec): Boolean
12 begin
13 Result := (TPath.MatchesPattern(SearchRec.Name, 'b*.*', False));
14 end;
15
16 for sPath in TDirectory.GetFiles('c:\temp', FilterPredicate) do
17 writeln(Format('Name = %s', [sPath]));
18end.
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:
1TDirectory = record
2 class function IsEmpty(const Path: string): Boolean; static;
3 class procedure Copy(const SourceDirName, DestDirName: string); static;
4 class procedure CreateDirectory(Path: string); static;
5 class procedure Delete(const Path: string; const Recursive: Boolean); overload; static;
6 class function Exists(const Path: string): Boolean; inline; static;
7 class function GetAttributes(const Path: string): TFileAttributes; inline; static;
8 class function GetCurrentDirectory: string; inline; static;
9…
10nebo již použité MatchesPattern
11end;
TPath
Obsahuje metody pro práci s cestou, tj. například:
1class function GetExtension(const FileName: string): string; static;
2 class function GetFileName(const FileName: string): string; inline; static;
3 class function GetFileNameWithoutExtension(const FileName: string): string; static;
4 class function GetFullPath(const Path: string): string; static;
5 class function GetInvalidFileNameChars: TCharArray; inline; static;
6 class function GetInvalidPathChars: TCharArray; inline; static;
7 class function GetPathRoot(const Path: string): string; static;
8 class function GetRandomFileName: string; static;
9 class function GetTempFileName: string; static;
10 class function GetTempPath: string; static;
TFile
A poslední pro práci se souborem jako načtení, vytvoření, nastavení atributů atd. Velmi užitečné.
1TFile.Copy(source, dest);
2TFile.Move(source, dest);
3
4source := 'c:\temp\t1.txt';
5dest := 'c:\temp\t2.txt';
6bak := 'c:\temp\t3.txt';
7TFile.Replace(source, dest, bak);
8
9var
10 path: string;
11 sw: TStreamWriter;
12begin
13 path := 'c:\temp\test.txt';
14 sw := TFile.CreateText(path);
15 sw.Write(123);
16 sw.Write('ABC');
17 sw.Close;
18end;
19
20var
21 path: string;
22begin
23 path := 'c:\temp\test.txt';
24 TFile.AppendAllText(path, 'NewString', TEncoding.UTF8);
25end;
26
27TFile.WriteAllText(path, '123', TEncoding.UTF8);
28
29str := TFile.ReadAllText(path, TEncoding.UTF8);
30
31var
32 arr: TStringDynArray;
33…
34 arr := TFile.ReadAllLines(path);
35 arr := TFile.ReadAllLines(path, TEncoding.UTF8);
36
37TFile.Encrypt();
38TFile.Decrypt();
a další. Speciálně upozorňuji na TEncoding, tím se dá specifikovat kódování při práci se soubory.