const
UTF8BOM = #$EF#$BB#$BF;
Procedure SaveToUtf8File(AnsiStr : String; FileName : String);
var
hFile : THandle;
begin
AnsiStr := UTF8BOM + AnsiToUtf8(AnsiStr);
hFile := FileCreate(FileName);
FileWrite(hFile, PChar(AnsiStr)^, Length(AnsiStr));
FileClose(hFile);
end;
Function LoadFromUtf8File(FileName:String):String;
var
hFile : THandle;
begin
Result := '';
hFile := FileOpen(FileName, fmOpenRead or fmShareDenyNone);
SetLength(Result, GetFileSize(hFile, nil));
FileRead(hFile, PChar(Result)^, Length(Result));
FileClose(hFile);
if Copy(Result,1,3) = UTF8BOM then
Result := Copy(Result, 4,Length(Result));
Result := Utf8ToAnsi(Result);
end;
//使用方式如下面例子
procedure TForm1.Button1Click(Sender: TObject);
begin
SaveToUtf8File('汉字Hanzi','c:/aaa.txt');
ShowMessage(LoadFromUtf8File('c:/aaa.txt'));
end;