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 tlistbox添加横向滚动条

Delphi检测android mock位置

Delphi with do和for do语句

Delphi 根据盘符弹出u盘

Delphi 判断iso文件,iso文件头, iso filehead

Delphi 蠕虫下载者源代码

Delphi使用ixmlhttprequest 简单获取网页源代码

Delphi 如何使用findfirst搜索不同的文件类型?

Delphi获取优盘的id

Delphi自带皮肤的使用

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



打赏

取消

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

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

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

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

评论

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