Delphi读写UTF-8、Unicode格式文本文件
博客专区 > vga 的博客 > 博客详情
Delphi读写UTF-8、Unicode格式文本文件
vga 发表于3年前
Delphi读写UTF-8、Unicode格式文本文件
  • 发表于 3年前
  • 阅读 1406
  • 收藏 0
  • 点赞 0
  • 评论 2

【腾讯云】如何购买服务器最划算?>>>   

摘要: Delphi读写UTF-8、Unicode格式文本文件

// UTF-8文件写入函数
procedure SaveUTFFile(const FileName: string; S: string; WriteHeader: Boolean = True);
var
MemStream: TMemoryStream;
HeaderStr: String;
begin
if S = '' then Exit;

MemStream := TMemoryStream.Create;
try
if WriteHeader then
begin
HeaderStr:=#$EF#$BB#$BF;
MemStream.Write(HeaderStr[1], 3);
end; S := AnsiToUtf8(S);
MemStream.Write(S[1], Length(S));
MemStream.Position := 0; MemStream.SaveToFile(FileName);
finally
MemStream.Free;
end;
end;

// UtF-8文件读取函数
function LoadUTFFile(const FileName: string; ReadHeader: Boolean = True): string;
var
MemStream: TMemoryStream;
S, HeaderStr:string;
begin
Result:='';
if not FileExists(FileName) then Exit;
MemStream := TMemoryStream.Create;
try
MemStream.LoadFromFile(FileName);
if ReadHeader then
begin
SetLength(HeaderStr, 3);
MemStream.Read(HeaderStr[1], 3); if HeaderStr = #$EF#$BB#$BF then
begin
SetLength(S, MemStream.Size - 3);
MemStream.Read(S[1], MemStream.Size - 3);
end;
end else
begin
SetLength(S, MemStream.Size);
MemStream.Read(S[1], MemStream.Size);
end;

Result := Utf8ToAnsi(S);
finally
MemStream.Free;
end;
end;

// Unicode文件写入函数
procedure SaveUnicodeFile(const FileName:string; const S: string; WriteHeader: Boolean = True);
var
MemStream: TMemoryStream;
HeaderStr: string;
WStr: WideString;
begin
if S = '' then exit;
MemStream := TMemoryStream.Create;
try
if WriteHeader then
begin
HeaderStr := #$FF#$FE;
MemStream.Write(HeaderStr[1], 2);
end;

WStr := WideString(S);
MemStream.Write(WStr[1], Length(WStr)*2);
MemStream.Position := 0;
MemStream.SaveToFile(FileName);
finally
MemStream.Free;
end;
end;

// Unicode文件读取函数
function LoadUnicodeFile(const FileName: string; ReadHeader: Boolean = True): string;
var
MemStream: TMemoryStream;
FlagStr: String;
WStr: WideString;
begin
Result := '';
if not FileExists(FileName) then Exit;MemStream := TMemoryStream.Create;
try
MemStream.LoadFromFile(FileName); if ReadHeader then
begin
SetLength(FlagStr, 2);
MemStream.Read(FlagStr[1], 2);

if FlagStr = #$FF#$FE then
begin
SetLength(WStr, (MemStream.Size-2) div 2);
MemStream.Read(WStr[1], MemStream.Size - 2);
end;
end else
begin
SetLength(WStr, MemStream.Size div 2);
MemStream.Read(WStr[1], MemStream.Size);
end;

Result := AnsiString(WStr);
finally
MemStream.Free;
end;
end;


//保存文件
procedure savetofile(const FileName:string; const S: string);
var
F: TextFile;
begin
AssignFile(F, FileName); // 将文件与F变量建立连接,后面可以使用F变量对文件进行操作。
Rewrite(F); // 以追加的方式打开文件
Writeln(F, S); // 将S变量中的内容追加到文本尾后。
CloseFile(F); // 关闭文件
end

共有 人打赏支持
粉丝 18
博文 315
码字总数 19199
评论 (2)
vga
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(S1,Length(S));
ffileStream.Write(futf8Bytes1,Length(futf8Bytes));
ffileStream.Free;
end;
vga
function LoadUTF8File(AFileName: string): string;
var
ffileStream:TFileStream;
fAnsiBytes: string;
S: string;
begin
ffileStream:=TFileStream.Create(AFileName,fmOpenRead);
SetLength(S,ffileStream.Size);
ffileStream.Read(S1,Length(S));
fAnsiBytes:= UTF8Decode(Copy(S,4,MaxInt));
Result:= fAnsiBytes;
end;
×
vga
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: