2021-04-03

【Delphi7】儲存寫入 UTF-8 文字檔

參考資料 ----

BOM

 

 

本篇筆記的做法所儲存的文字檔,預設不含 BOM,若要為含 BOM 的文字檔,則需在一開始要寫入文字檔時,加入 BOM 記號。

 

 

 
const
    sFilename = 'd:\要存放文字檔的目錄\uf8write.txt';
var
    myfile: TextFile;
    ss: string;
    xx: integer;
begin
    Screen.Cursor := crHourGlass;

    // 若文字檔不存在, 則新建文字檔
    if not FileExists(sFilename) then
    begin
        AssignFile(myfile, sFilename);
        ReWrite(myfile);
        CloseFile(myfile);
    end;

    AssignFile(myfile, sFilename);
    ReWrite(myfile);

    xx := 0;
    // 將 memo1 的內容逐列寫入文字檔
    while (xx<memo1.Lines.Count) do
    begin
        // 檔首寫 BOM
        if (xx=0) then
            begin
                ss := #$EF#$BB#$BF + AnsiToUTF8(memo1.Lines[xx]);
            end
        else
            begin
                ss := AnsiToUTF8(memo1.Lines[xx]);
            end;
        WriteLn(myfile, ss);
        xx := xx + 1;
    end;

    Flush(myfile);
    CloseFile(myfile);
    Screen.Cursor := crDefault;
    Showmessage('ok');
end;
 



相關筆記 ----

【Delphi7】讀取 UTF-8 文字檔



沒有留言:

張貼留言