Delphi读取和写入utf-8编码格式的文件

2019-10-3021:32:55来源: 百炼成钢百忍成佛 评论 1,177 views

读取UTF-8格式的文件内容

function LoadUTF8File(AFileName: string): string;
var  ffileStream:TFileStream; 
         fAnsiBytes: string;
         S: string;
begin
            ffileStream:=TFileStream.Create(AFileName,fmOpenRead);
            SetLength(S,ffileStream.Size); 
            ffileStream.Read(S[1],Length(S));
            fAnsiBytes:= UTF8Decode(Copy(S,4,MaxInt));
           Result:= fAnsiBytes;
end;

写入UTF-8编码格式的文件

procedure SaveUTF8File(AContent:string;AFileName: string);
var ffileStream:TFileStream;
        futf8Bytes: string;
       S: string;
begin
       ffileStream:=TFileStream.Create(AFileName,fmCreate); 
       futf8Bytes:= UTF8Encode(AContent); 
       S:=#$EF#$BB#$BF; 
       ffileStream.Write(S[1],Length(S)); 
       ffileStream.Write(futf8Bytes[1],Length(futf8Bytes)); 
       ffileStream.Free;
end;
历史上的今天
10 月
30

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: