Delphi XE5实现通过TMemoryStream将一个UnicodeString写入到一个Unicode文本文件


本文整理自网络,侵删。

 

本文章介绍了Delphi XE5实现通过TMemoryStream将一个UnicodeString写入到一个Unicode文本文件,之前研究了通过TMemoryStream将UnicodeString写入到文本文件,但是打开后,是乱码,是因为文本文件的字符表示为Ansi,所以写进去打开有问题,当然也可以通过TStrings的SaveToFile来保存成Unicode,但是TMemoStream的SaveToFile没有这样的功能,所以需要提前将Unicode标志写入到文本文件中,下面是代码:


procedure SaveUnicodeA(f:string;s:string);

var

  Buffer, Preamble: TBytes;

  ms:TMemoryStream;

  ws:WideString;

  hs:array[0..1]of char;

begin

  if s='' then exit;

  try

    ms:=TMemoryStream.Create;

    //Buffer := TEncoding.Unicode.GetBytes(s);

    Preamble := TEncoding.Unicode.GetPreamble;

    if Length(Preamble) > 0 then

      ms.WriteBuffer(Preamble, Length(Preamble));

    ms.WriteBuffer(PChar(s)^,length(s)*2);

    ms.SaveToFile(f);

  finally

    ms.Free;

  end;

end;

相关阅读 >>

Delphi edit,memo中禁用ctrl+v

Delphi2007-Delphi2010 程序不出现在任务栏的方法

Delphi mediaplayer1 播放avi 视频

Delphi idftp ditdirectory未定义

Delphi 10.4改进 内联变量声明

Delphi 读取文本文件的最后一行

Delphi sysutils.trim、sysutils.trimleft、sysutils.trimright - 删除空格

Delphi xe8 numberbox使用方法及存在的问题

Delphi 批量给每个字符加括号

Delphi vclskin 5.40在2010安装方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...