Delphi读写UTF-8Unicode格式文本文件

Posted 朝闻道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Delphi读写UTF-8Unicode格式文本文件相关的知识,希望对你有一定的参考价值。

// 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;
 
http://blog.csdn.net/missmecn/article/details/3007964

以上是关于Delphi读写UTF-8Unicode格式文本文件的主要内容,如果未能解决你的问题,请参考以下文章

彻底弄懂UTF-8Unicode宽字符locale

[转帖]彻底弄懂UTF-8Unicode宽字符locale

delphi保文本文件怎么选择编码UTF-8格式

#yyds干货盘点#Golang 字符编码UTF-8Unicode之间的关系

在c语言中怎样打开一个文本文档

delphi写多行文本文件操作