博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TStringList.SaveToStream TStringStream.SaveToStream
阅读量:6896 次
发布时间:2019-06-27

本文共 1853 字,大约阅读时间需要 6 分钟。

There are two methods widely used in Delphi code for reading and writing strings to/from streams with Delphi,

that initially seem pretty similar in their behaviour. 

These are TStrings.SaveToStream and TStringStream.SaveToStream (or SaveToFile in either case):

procedure TForm1.SaveToFile;const  AString = 'This is some Unicode text'#13 + 'Test Unicode © Δ א';begin  with TStringList.Create do  try    Text := AString;    SaveToFile('TStringList UTF8.txt', TEncoding.UTF8);  finally    Free;  end;  with TStringStream.Create(AString, TEncoding.UTF8) do  try    SaveToFile('TStringStream UTF8.txt');  finally    Free;  end;end;

But there are several crucial differences in what is written to the stream between these two methods:

  1. TStringList prepends the preamble bytes for the encoding (in this case, #$EF#$BB#$BF)
  2. TStringList appends a new line #$0D#$0A to the file, if your text does not already end in a new line.
  3. TStringList converts any single line breaking characters in the text (e.g. #$0D or#$0A) into #$0D#$0A.

The following hex dumps may show this more clearly:

Insdert
EF BB BF
 54 68 69 73 20 69 73 20 73 6F 6D 65 20 
55 6E 69 63 6F 64 65 20 74 65 78 74 
0D 0A 54 65
<-- converted from 0x0D
73 74 20 55 6E 69 63 6F 64 65 20 C2 A9 20 CE 94 
20 D7 90
 0D 0A <-- append
TStringList UTF8.txt

54 68 69 73 20 69 73 20 73 6F 6D 65 20 55 6E 69 
63 6F 64 65 20 74 65 78 74 
0D 54 65 73 74 20 55 
6E 69 63 6F 64 65 20 C2 A9 20 CE 94 20 D7 90 
TStringStream UTF8.txt

Make sure you know how your files will be read and whether these differences are important to the target application.

Basically, TStringList is typically not appropriate for streaming strings without modification. 

TStringStream is your friend here. 

But if you need the preamble, and just the preamble, then you'll have to do a little more work;

you won't be able to use TStringStream.SaveToFile.

 

转载地址:http://dwddl.baihongyu.com/

你可能感兴趣的文章
怎样看K线图(实图详解)
查看>>
JSON 转javabean 利器
查看>>
基于W5500+Yeelink的远程灯光控制设计
查看>>
Notes中几个处理多值域的通用函数
查看>>
量化生产力Quantifying Productivity
查看>>
趣文:我是一个线程
查看>>
iOS - UIAlertView
查看>>
【资料下载区】【iCore3相关代码、资料下载地址】更新日期2017/06/28
查看>>
短信发送的流程,硬编码在了服务方法里面,优化方案
查看>>
tcpdump
查看>>
maven的pom文件中配置测试用例
查看>>
Swift 方法
查看>>
angularjs等号运算
查看>>
LeetCode: Symmetric Tree 解题报告
查看>>
C# 线程手册 第五章 扩展多线程应用程序 CLR 和 线程
查看>>
html a name href
查看>>
JavaScript中json对象和string对象之间相互转化
查看>>
arm程序的反汇编程序
查看>>
SQL Server 2008数据库的一些基本概念
查看>>
在ASP.NET中重写URL
查看>>