參考資料 ----
Delphi 預設讀取 ASCII 文字檔,若要讀取 UTF-8 格式的文字檔,則需要轉換。
在 Form1 上放一個 button 和 memo,然後在 button onclick event 寫入以下程式碼:
procedure TForm1.Button1Click(Sender: TObject);
var
myfile: TextFile;
ss: string;
begin
memo1.clear;
AssignFile(myfile, 'd:\temp\list.csv');
Reset(myfile);
while not EOF(myfile) do
begin
ReadLn(myfile,ss);
// 由於某些 UTF-8 格式的檔首會含有 BOM
// 但 BOM 對 UTF-8 文字檔並非必需
// 因此將 BOM 字元消除
if (pos(#$EF#$BB#$BF,ss)>0) then
begin
ss := StringReplace(ss, #$EF#$BB#$BF, '', [rfReplaceAll]);
end;
ss := UTF8ToAnsi(ss);
memo1.Lines.Add(ss);
end;
CloseFile(myfile);
end;
相關筆記 ----