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

本文介绍了 Delphi 2010 中 IOUtils 单元的 TFile 结构,包括 TFile.Exists()、TFile.Copy()、TFile.Move()、TFile.Delete()、TFile.Replace()、TFile.Create()、TFile.OpenWrite()、TFile.OpenRead()等方法,详述了各种文件操作的功能和用法。
摘要由CSDN通过智能技术生成
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, Si
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值