OOP file access in Delphi 2010

by Radek Červinka 20. January 2011 21:59

Starting with Delphi 2010 there are some new classes for file access .

In unit IOUtils there are this classes (in fact this is extended records and not classes - Delphi from about version 2006 allow methods on records):

  • TDirectory
  • TPath
  • TFile

This object model is similar to .NET.

TDirectory

This record has methods for working with directory, directory search, obtaining file list, creating, deleting or obtaining last access time - total about 120 methods.

    1program Project1;
    2{$APPTYPE CONSOLE}
    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
   12or 
   13  if not TDirectory.Exists(edtPath.Text) then 
   14    blabla

This was simple. More interesting is next example.

    1program Project1;
    2{$APPTYPE CONSOLE}
    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.

We defined anonymous method FilterPredicate and use to in GetFiles. This method returns TStringDynArray and we iterate through with for - in.

Important methods are:

    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   10or already used MatchesPattern
   11end;

TPath

Contains methods for work with Path, for example:

    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

For working with file we can use TFile. Like read file, create file or set attributes… Very nice.

    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();

Special note for the TEncoding: it can specify the encoding when working with files.

Tags: ,

Review

Comments are closed