2021-04-03

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

參考資料 ----

BOM

 

 

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

 

 

  1.  
  2. const
  3. sFilename = 'd:\要存放文字檔的目錄\uf8write.txt';
  4. var
  5. myfile: TextFile;
  6. ss: string;
  7. xx: integer;
  8. begin
  9. Screen.Cursor := crHourGlass;
  10.  
  11. // 若文字檔不存在, 則新建文字檔
  12. if not FileExists(sFilename) then
  13. begin
  14. AssignFile(myfile, sFilename);
  15. ReWrite(myfile);
  16. CloseFile(myfile);
  17. end;
  18.  
  19. AssignFile(myfile, sFilename);
  20. ReWrite(myfile);
  21.  
  22. xx := 0;
  23. // 將 memo1 的內容逐列寫入文字檔
  24. while (xx<memo1.Lines.Count) do
  25. begin
  26. // 檔首寫 BOM
  27. if (xx=0) then
  28. begin
  29. ss := #$EF#$BB#$BF + AnsiToUTF8(memo1.Lines[xx]);
  30. end
  31. else
  32. begin
  33. ss := AnsiToUTF8(memo1.Lines[xx]);
  34. end;
  35. WriteLn(myfile, ss);
  36. xx := xx + 1;
  37. end;
  38.  
  39. Flush(myfile);
  40. CloseFile(myfile);
  41. Screen.Cursor := crDefault;
  42. Showmessage('ok');
  43. end;
  44.  



相關筆記 ----

【Delphi7】讀取 UTF-8 文字檔



沒有留言:

張貼留言