乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Delphi 2010 新增功能之: IOUtils 單元(7): TFile 結(jié)構(gòu)的功能...

       獨(dú)孤求財(cái) 2012-03-30

      IOUtils 單元主要就是三個(gè)結(jié)構(gòu): TDirectory、TPath、TFile, 很有用; 下面是 TFile 的功能簡(jiǎn)介.

      TFile.Exists();
      //判斷指定的文件是否存在
      

      TFile.Copy();
      //復(fù)制文件
      var
        source,dest: string;
      begin
        TFile.Copy(source, dest);       {不允許覆蓋同名的文件}
        TFile.Copy(source, dest, True); {將覆蓋同名的文件}
      end;

      TFile.Move();
      //移動(dòng)文件
      var
        source,dest: string;
      begin
        TFile.Move(source, dest);
      end;
      

      TFile.Delete();
      //刪除文件
      

      TFile.Replace();
      //替換文件, 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;
      

      TFile.Create();
      //建立文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStream, 指定文件存在則覆蓋
      var
        buf: array[0..1023] of Byte;
        fs: TFileStream;
      begin
        {模擬一個(gè)緩沖區(qū)并填充}
        FillChar(buf, SizeOf(buf), 65);
      
        {使用返回的 TFileStream 寫(xiě)入流}
        fs := TFile.Create('c:\temp\test1.txt');
        fs.Write(buf, SizeOf(buf));
        fs.Free;
      
        {如果已知要寫(xiě)入流的大小, 可以使用第二個(gè)參數(shù)指定, 這樣會(huì)快一點(diǎn)}
        fs := TFile.Create('c:\temp\test2.txt', SizeOf(buf));
        fs.Write(buf, SizeOf(buf));
        fs.Free;
      end;
      

      TFile.OpenWrite();
      //按只寫(xiě)權(quán)限打開(kāi)文件并返回一個(gè)和文件關(guān)聯(lián)的 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();
      //按只讀權(quán)限打開(kāi)文件并返回一個(gè)和文件關(guān)聯(lián)的 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();
      //打開(kāi)文件并返回一個(gè)和文件關(guān)聯(lián)的 TFileStream
      var
        path: string;
        fs: TFileStream;
      begin
        path := 'c:\temp\test.dat';        {文件要存在}
      
        //重載一: 指定打開(kāi)模式; 默認(rèn)操作權(quán)限是 faReadWrite, 默認(rèn)線程訪問(wèn)權(quán)限是 fsNone
        fs := TFile.Open(path, TFileMode);
      
        //重載二: 指定打開(kāi)模式、操作權(quán)限; 默認(rèn)線程訪問(wèn)權(quán)限是 fsNone
        fs := TFile.Open(path, TFileMode, TFileAccess);
      
        //重載三: 指定打開(kāi)模式、操作權(quán)限和其他線程的訪問(wèn)權(quán)限
        fs := TFile.Open(path, TFileMode, TFileAccess, TFileShare);
      
      { TFileMode 打開(kāi)模式:
        TFileMode.fmCreateNew    創(chuàng)建新文件, 如果文件已存在則將引發(fā)異常;
        TFileMode.fmCreate       創(chuàng)建新文件, 如果文件已存在則覆蓋;
        TFileMode.fmOpen         打開(kāi)現(xiàn)有文件, 如果該文件不存在則將引發(fā)異常;
        TFileMode.fmOpenOrCreate 打開(kāi)文件, 如果文件不存在則建新文件;
        TFileMode.fmTruncate     打開(kāi)現(xiàn)有文件并清空;
        TFileMode.fmAppend       打開(kāi)現(xiàn)有文件并把流指針移到文件尾, 如果文件不存在創(chuàng)建新文件.
      }
      { TFileMode 操作權(quán)限:
        TFileMode.faRead      只讀;
        TFileMode.faWrite     只寫(xiě);
        TFileMode.faReadWrite 可讀寫(xiě).
      }
      { TFileShare 對(duì)其他線程的訪問(wèn)限制:
        TFileMode.fsNone      禁止其他線程共享;
        TFileMode.fsRead      允許其他線程讀;
        TFileMode.fsWrite     允許其他線程寫(xiě);
        TFileMode.fsReadWrite 允許其他線程讀寫(xiě).
      }
      end;
      

      TFile.CreateText();
      //建立文本文件, 存在則覆蓋; 會(huì)返回 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();
      //為追加而打開(kāi)文本文件, 不存在則創(chuàng)建; 會(huì)返回 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();
      //打開(kāi)文本文件, 追加文本后關(guān)閉; 文件不存在則創(chuàng)建.
      var
        path: string;
      begin
        path := 'c:\temp\test.txt';
        TFile.AppendAllText(path, 'NewString');
        TFile.AppendAllText(path, 'NewString', TEncoding.UTF8); {可指定編碼格式}
      end;
      

      TFile.OpenText();
      //打開(kāi)文本文件, 返回 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();
      //打開(kāi)文本文件, 寫(xiě)入指定文本后關(guān)閉; 不管文件存在與否都將覆蓋!
      var
        path: string;
      begin
        path := 'c:\temp\test.txt';
        TFile.WriteAllText(path, '123');
        TFile.WriteAllText(path, '123', TEncoding.UTF8); {可指定編碼格式}
      end;
      

      TFile.WriteAllLines();
      //打開(kāi)文本文件, 寫(xiě)入指定的字符串?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;
      

      TFile.WriteAllBytes();
      //打開(kāi)文本文件, 寫(xiě)入指定的 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;
      

      TFile.ReadAllText();
      //打開(kāi)文本文件, 全部讀取字符串變量后關(guān)閉.
      var
        path: string;
        str: string;
      begin
        path := 'c:\temp\test.txt';
        str := TFile.ReadAllText(path);
        str := TFile.ReadAllText(path, TEncoding.UTF8); {可指定編碼格式}
      end;
      

      TFile.ReadAllLines();
      //打開(kāi)文本文件, 全部讀入到字符串?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;
      

      TFile.ReadAllBytes();
      //打開(kāi)文本文件, 全部讀入到 TBytes 數(shù)組后關(guān)閉;
      var
        path: string;
        bs: TBytes;
      begin
        path := 'c:\temp\test.txt';
        bs := TFile.ReadAllBytes(path);
        ShowMessage(IntToStr(Length(bs)));
      end;
      

      暫時(shí)測(cè)試有問(wèn)題的方法:
      TFile.Encrypt(); {加密文件}
      TFile.Decrypt(); {解密文件}
      

      其他方法:
      {讀取和設(shè)置屬性的方法前面有過(guò)例子}
      TFile.GetAttributes();
      TFile.SetAttributes();
      
      {讀取和設(shè)置文件的建立時(shí)間、最后寫(xiě)入時(shí)間、最后訪問(wèn)時(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();
      

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多