从文件中读取/保存Unicode/ UTF-8字符串,读取为string/widestring
unit UnitReadWriteUtfUnicodeFile;
interface
uses
Classes, SysUtils;
procedure SaveUTF(f: string; s: string; b: boolean = true);
function LoadUTF(f: string; b: boolean = true): WideString;
procedure SaveUnicode(f: string; s: string; b: boolean = true);
function LoadUnicode(f: string; b: boolean = true): WideString;
implementation
procedure SaveUTF(f: string; s: string; b: boolean = true);
var
ms:TMemoryStream;
hs:String;
begin
if s='' then exit;
ms:=TMemoryStream.Create;
if b then begin
hs:=#$EF#$BB#$BF;
ms.Write(hs[1],3);
end;
s:=AnsiToUtf8(s);
ms.Write(s[1],Length(s));
ms.Position:=0;
ms.SaveToFile(f);
ms.Free;
end;
function LoadUTF(f: string; b: boolean = true): WideString;
var
ms:TMemoryStream;
s,hs:string;
begin
Result:='';
if not FileExists(f) then exit;
ms:=TMemoryStream.Create;
ms.LoadFromFile(f);
if b then begin
SetLength(hs,3);
ms.Read(hs[1],3);
if hs<>#$EF#$BB#$BF then begin ms.Free; exit; end;
SetLength(s,ms.Size-3);
ms.Read(s[1],ms.Size-3);
end else begin
SetLength(s,ms.Size);
ms.Read(s[1],ms.Size);
end;
Result:=Utf8ToAnsi(s);
ms.Free;
end;
procedure SaveUnicode(f: string; s: string; b: boolean = true);
var
ms:TMemoryStream;
hs:string;
ws:WideString;
begin
if s='' then exit;
ms:=TMemoryStream.Create;
if b then begin
hs:=#$FF#$FE;
ms.Write(hs[1],2);
end;
ws:=WideString(s);
ms.Write(ws[1],Length(ws)*2);
ms.Position:=0;
ms.SaveToFile(f);
ms.Free;
end;
function LoadUnicode(f: string; b: boolean = true): WideString;
var
ms:TMemoryStream;
hs:String;
ws:WideString;
begin
Result:='';
if not FileExists(f) then exit;
ms:=TMemoryStream.Create;
ms.LoadFromFile(f);
if b then begin
SetLength(hs,2);
ms.Read(hs[1],2);
if hs<>#$FF#$FE then begin ms.Free; exit; end;
SetLength(ws,(ms.Size-2) div 2);
ms.Read(ws[1],ms.Size-2);
end else begin
SetLength(ws,ms.Size div 2);
ms.Read(ws[1],ms.Size);
end;
Result:=AnsiString(ws);
ms.Free;
end;
end.