//判斷指定的文件是否存在
//復(fù)制文件var source,dest: string;begin TFile.Copy(source, dest); {不允許覆蓋同名的文件} TFile.Copy(source, dest, True); {將覆蓋同名的文件}end;
//移動(dòng)文件var source,dest: string;begin TFile.Move(source, dest);end;
//刪除文件
//替換文件, dest 會(huì)備份在 bak, 復(fù)制 source 的內(nèi)容到 dest 后, sourece 會(huì)被刪除.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); {前兩個(gè)文件必須存在} TFile.Replace(source, dest, bak, True); {忽略錯(cuò)誤}end;
//建立文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStream, 指定文件存在則覆蓋var buf: array[0..1023] of Byte; fs: TFileStream;begin {模擬一個(gè)緩沖區(qū)并填充} FillChar(buf, SizeOf(buf), 65); {使用返回的 TFileStream 寫入流} fs := TFile.Create('c:\temp\test1.txt'); fs.Write(buf, SizeOf(buf)); fs.Free; {如果已知要寫入流的大小, 可以使用第二個(gè)參數(shù)指定, 這樣會(huì)快一點(diǎn)} fs := TFile.Create('c:\temp\test2.txt', SizeOf(buf)); fs.Write(buf, SizeOf(buf)); fs.Free;end;
//按只寫權(quán)限打開文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStreamconst 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;
//按只讀權(quán)限打開文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStreamvar path: string; fs: TFileStream;begin path := 'c:\temp\test.dat'; {文件要存在} fs := TFile.OpenRead(path); ShowMessage(IntToStr(fs.Size)); fs.Free;end;
//打開文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStreamvar path: string; fs: TFileStream;begin path := 'c:\temp\test.dat'; {文件要存在} //重載一: 指定打開模式; 默認(rèn)操作權(quán)限是 faReadWrite, 默認(rèn)線程訪問權(quán)限是 fsNone fs := TFile.Open(path, TFileMode); //重載二: 指定打開模式、操作權(quán)限; 默認(rèn)線程訪問權(quán)限是 fsNone fs := TFile.Open(path, TFileMode, TFileAccess); //重載三: 指定打開模式、操作權(quán)限和其他線程的訪問權(quán)限 fs := TFile.Open(path, TFileMode, TFileAccess, TFileShare);{ TFileMode 打開模式: TFileMode.fmCreateNew 創(chuàng)建新文件, 如果文件已存在則將引發(fā)異常; TFileMode.fmCreate 創(chuàng)建新文件, 如果文件已存在則覆蓋; TFileMode.fmOpen 打開現(xiàn)有文件, 如果該文件不存在則將引發(fā)異常; TFileMode.fmOpenOrCreate 打開文件, 如果文件不存在則建新文件; TFileMode.fmTruncate 打開現(xiàn)有文件并清空; TFileMode.fmAppend 打開現(xiàn)有文件并把流指針移到文件尾, 如果文件不存在創(chuàng)建新文件.}{ TFileMode 操作權(quán)限: TFileMode.faRead 只讀; TFileMode.faWrite 只寫; TFileMode.faReadWrite 可讀寫.}{ TFileShare 對(duì)其他線程的訪問限制: TFileMode.fsNone 禁止其他線程共享; TFileMode.fsRead 允許其他線程讀; TFileMode.fsWrite 允許其他線程寫; TFileMode.fsReadWrite 允許其他線程讀寫.}end;
//建立文本文件, 存在則覆蓋; 會(huì)返回 TStreamWritervar path: string; sw: TStreamWriter;begin path := 'c:\temp\test.txt'; sw := TFile.CreateText(path); {使用的是 UTF8 格式} sw.Write(123); sw.Write('ABC'); sw.Close;end;
//為追加而打開文本文件, 不存在則創(chuàng)建; 會(huì)返回 TStreamWritervar path: string; sw: TStreamWriter;begin path := 'c:\temp\test.txt'; sw := TFile.AppendText(path); {使用的是 UTF8 格式} sw.Write(123); sw.Write('ABC'); sw.Close;end;
//打開文本文件, 追加文本后關(guān)閉; 文件不存在則創(chuàng)建.var path: string;begin path := 'c:\temp\test.txt'; TFile.AppendAllText(path, 'NewString'); TFile.AppendAllText(path, 'NewString', TEncoding.UTF8); {可指定編碼格式}end;
//打開文本文件, 返回 TStreamReader.var path: string; sr: TStreamReader;begin path := 'c:\temp\test.txt'; sr := TFile.OpenText(path); {將使用 UTF8 格式} ShowMessage(sr.ReadLine); sr.Close;end;
//打開文本文件, 寫入指定文本后關(guān)閉; 不管文件存在與否都將覆蓋!var path: string;begin path := 'c:\temp\test.txt'; TFile.WriteAllText(path, '123'); TFile.WriteAllText(path, '123', TEncoding.UTF8); {可指定編碼格式}end;
//打開文本文件, 寫入指定的字符串?dāng)?shù)組后關(guān)閉; 不管文件存在與否都將覆蓋!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;
//打開文本文件, 寫入指定的 TBytes 數(shù)組后關(guān)閉; 不管文件存在與否都將覆蓋!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;
//打開文本文件, 全部讀取字符串變量后關(guān)閉.var path: string; str: string;begin path := 'c:\temp\test.txt'; str := TFile.ReadAllText(path); str := TFile.ReadAllText(path, TEncoding.UTF8); {可指定編碼格式}end;
//打開文本文件, 全部讀入到字符串?dāng)?shù)組后關(guān)閉.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;
//打開文本文件, 全部讀入到 TBytes 數(shù)組后關(guān)閉;var path: string; bs: TBytes;begin path := 'c:\temp\test.txt'; bs := TFile.ReadAllBytes(path); ShowMessage(IntToStr(Length(bs)));end;
TFile.Encrypt(); {加密文件}TFile.Decrypt(); {解密文件}
{讀取和設(shè)置屬性的方法前面有過例子}TFile.GetAttributes();TFile.SetAttributes();{讀取和設(shè)置文件的建立時(shí)間、最后寫入時(shí)間、最后訪問時(shí)間(分別有本地和UTC兩種時(shí)間格式)}TFile.GetCreationTime();TFile.GetCreationTimeUtc();TFile.GetLastAccessTime();TFile.GetLastAccessTimeUtc();TFile.GetLastWriteTime();TFile.GetLastWriteTimeUtc();TFile.SetCreationTime();TFile.SetCreationTimeUtc();TFile.SetLastAccessTime();TFile.SetLastAccessTimeUtc();TFile.SetLastWriteTime();TFile.SetLastWriteTimeUtc();
聯(lián)系客服