Delphi 2010 新增功能之: IOUtils 单元(7): TFile 结构的功能


IOUtils 单元主要就是三个结构: TDirectory、TPath、TFile, 很有用; 下面是 TFile 的功能简介.

TFile.Exists();

//判断指定的文件是否存在


 
   

TFile.Copy();

//复制文件

var

  source,dest: string;

begin

  TFile.Copy(source, dest);       {不允许覆盖同名的文件}

  TFile.Copy(source, dest, True); {将覆盖同名的文件}

end;
 
   

TFile.Move();

//移动文件

var

  source,dest: string;

begin

  TFile.Move(source, dest);

end;


 
   

TFile.Delete();

//删除文件


 
   

TFile.Replace();

//替换文件, dest 会备份在 bak, 复制 source 的内容到 dest 后, sourece 会被删除.

var

  source,dest,bak: string;

begin

  source := 'c:\temp\t1.txt';

  dest   := 'c:\temp\t2.txt';

  bak    := 'c:\temp\t3.txt';

  TFile.Replace(source, dest, bak);       {前两个文件必须存在}

  TFile.Replace(source, dest, bak, True); {忽略错误}

end;


 
   

TFile.Create();

//建立文件并返回一个和文件关联的 TFileStream, 指定文件存在则覆盖

var

  buf: array[0..1023] of Byte;

  fs: TFileStream;

begin

  {模拟一个缓冲区并填充}

  FillChar(buf, SizeOf(buf), 65);



  {使用返回的 TFileStream 写入流}

  fs := TFile.Create('c:\temp\test1.txt');

  fs.Write(buf, SizeOf(buf));

  fs.Free;



  {如果已知要写入流的大小, 可以使用第二个参数指定, 这样会快一点}

  fs := TFile.Create('c:\temp\test2.txt', SizeOf(buf));

  fs.Write(buf, SizeOf(buf));

  fs.Free;

end;


 
   

TFile.OpenWrite();

//按只写权限打开文件并返回一个和文件关联的 TFileStream

const

  buf: array[0..2] of Char = ('A', 'B', 'C');

var

  path: string;

  fs: TFileStream;

begin

  path := 'c:\temp\test.dat';    {文件要存在}

  fs := TFile.OpenWrite(path);

  fs.Seek(0, TSeekOrigin.soEnd); {把流指针移到尾部}

  fs.Write(buf, Length(buf)*SizeOf(Char));

  fs.Free;

end;


 
   

TFile.OpenRead();

//按只读权限打开文件并返回一个和文件关联的 TFileStream

var

  path: string;

  fs: TFileStream;

begin

  path := 'c:\temp\test.dat';    {文件要存在}

  fs := TFile.OpenRead(path);

  ShowMessage(IntToStr(fs.Size));

  fs.Free;

end;


 
   

TFile.Open();

//打开文件并返回一个和文件关联的 TFileStream

var

  path: string;

  fs: TFileStream;

begin

  path := 'c:\temp\test.dat';        {文件要存在}



  //重载一: 指定打开模式; 默认操作权限是 faReadWrite, 默认线程访问权限是 fsNone

  fs := TFile.Open(path, TFileMode);



  //重载二: 指定打开模式、操作权限; 默认线程访问权限是 fsNone

  fs := TFile.Open(path, TFileMode, TFileAccess);



  //重载三: 指定打开模式、操作权限和其他线程的访问权限

  fs := TFile.Open(path, TFileMode, TFileAccess, TFileShare);



{ TFileMode 打开模式:

  TFileMode.fmCreateNew    创建新文件, 如果文件已存在则将引发异常;

  TFileMode.fmCreate       创建新文件, 如果文件已存在则覆盖;

  TFileMode.fmOpen         打开现有文件, 如果该文件不存在则将引发异常;

  TFileMode.fmOpenOrCreate 打开文件, 如果文件不存在则建新文件;

  TFileMode.fmTruncate     打开现有文件并清空;

  TFileMode.fmAppend       打开现有文件并把流指针移到文件尾, 如果文件不存在创建新文件.

}

{ TFileMode 操作权限:

  TFileMode.faRead      只读;

  TFileMode.faWrite     只写;

  TFileMode.faReadWrite 可读写.

}

{ TFileShare 对其他线程的访问限制:

  TFileMode.fsNone      禁止其他线程共享;

  TFileMode.fsRead      允许其他线程读;

  TFileMode.fsWrite     允许其他线程写;

  TFileMode.fsReadWrite 允许其他线程读写.

}

end;


 
   

TFile.CreateText();

//建立文本文件, 存在则覆盖; 会返回 TStreamWriter

var

  path: string;

  sw: TStreamWriter;

begin

  path := 'c:\temp\test.txt';

  sw := TFile.CreateText(path); {使用的是 UTF8 格式}

  sw.Write(123);

  sw.Write('ABC');

  sw.Close;

end;


 
   

TFile.AppendText();

//为追加而打开文本文件, 不存在则创建; 会返回 TStreamWriter

var

  path: string;

  sw: TStreamWriter;

begin

  path := 'c:\temp\test.txt';

  sw := TFile.AppendText(path); {使用的是 UTF8 格式}

  sw.Write(123);

  sw.Write('ABC');

  sw.Close;

end;


 
   

TFile.AppendAllText();

//打开文本文件, 追加文本后关闭; 文件不存在则创建.

var

  path: string;

begin

  path := 'c:\temp\test.txt';

  TFile.AppendAllText(path, 'NewString');

  TFile.AppendAllText(path, 'NewString', TEncoding.UTF8); {可指定编码格式}

end;


 
   

TFile.OpenText();

//打开文本文件, 返回 TStreamReader.

var

  path: string;

  sr: TStreamReader;

begin

  path := 'c:\temp\test.txt';

  sr := TFile.OpenText(path); {将使用 UTF8 格式}

  ShowMessage(sr.ReadLine);

  sr.Close;

end;


 
   

TFile.WriteAllText();

//打开文本文件, 写入指定文本后关闭; 不管文件存在与否都将覆盖!

var

  path: string;

begin

  path := 'c:\temp\test.txt';

  TFile.WriteAllText(path, '123');

  TFile.WriteAllText(path, '123', TEncoding.UTF8); {可指定编码格式}

end;


 
   

TFile.WriteAllLines();

//打开文本文件, 写入指定的字符串数组后关闭; 不管文件存在与否都将覆盖!

var

  path: string;

  arr: TStringDynArray; {这定义在 Types 单元}

begin

  SetLength(arr, 2);

  arr[0] := 'AAA';

  arr[1] := 'BBB';



  path := 'c:\temp\test.txt';

  TFile.WriteAllLines(path, arr);

  TFile.WriteAllLines(path, arr, TEncoding.UTF8); {可指定编码格式}

end;


 
   

TFile.WriteAllBytes();

//打开文本文件, 写入指定的 TBytes 数组后关闭; 不管文件存在与否都将覆盖!

var

  path: string;

  bs: TBytes;

begin

  SetLength(bs, 2);

  bs[0] := 65;

  bs[1] := 66;



  path := 'c:\temp\test.txt';

  TFile.WriteAllBytes(path, bs);

end;


 
   

TFile.ReadAllText();

//打开文本文件, 全部读取字符串变量后关闭.

var

  path: string;

  str: string;

begin

  path := 'c:\temp\test.txt';

  str := TFile.ReadAllText(path);

  str := TFile.ReadAllText(path, TEncoding.UTF8); {可指定编码格式}

end;


 
   

TFile.ReadAllLines();

//打开文本文件, 全部读入到字符串数组后关闭.

var

  path: string;

  arr: TStringDynArray; {这定义在 Types 单元}

begin

  path := 'c:\temp\test.txt';

  arr := TFile.ReadAllLines(path);

  arr := TFile.ReadAllLines(path, TEncoding.UTF8); {可指定编码格式}

  ShowMessage(arr[0]);

end;


 
   

TFile.ReadAllBytes();

//打开文本文件, 全部读入到 TBytes 数组后关闭;

var

  path: string;

  bs: TBytes;

begin

  path := 'c:\temp\test.txt';

  bs := TFile.ReadAllBytes(path);

  ShowMessage(IntToStr(Length(bs)));

end;


 
   

暂时测试有问题的方法:

TFile.Encrypt(); {加密文件}

TFile.Decrypt(); {解密文件}


 
   

其他方法:

{读取和设置属性的方法前面有过例子}

TFile.GetAttributes();

TFile.SetAttributes();



{读取和设置文件的建立时间、最后写入时间、最后访问时间(分别有本地和UTC两种时间格式)}

TFile.GetCreationTime();

TFile.GetCreationTimeUtc();

TFile.GetLastAccessTime();

TFile.GetLastAccessTimeUtc();

TFile.GetLastWriteTime();

TFile.GetLastWriteTimeUtc();

TFile.SetCreationTime();

TFile.SetCreationTimeUtc();

TFile.SetLastAccessTime();

TFile.SetLastAccessTimeUtc();

TFile.SetLastWriteTime();

TFile.SetLastWriteTimeUtc();


 
   

你可能感兴趣的:(Delphi)