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

本文共 1924 字,大约阅读时间需要 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.

 

转载于:https://www.cnblogs.com/shangdawei/archive/2013/04/28/3049106.html

你可能感兴趣的文章
day62 中间件
查看>>
java 获取主机IP
查看>>
Ubuntu 开机自动执行脚本
查看>>
Asp.Net MVC CodeFirst模式数据库迁移步骤
查看>>
PHP线程安全和非线程安全的区别
查看>>
js数组对象的常用方法
查看>>
了解大数据
查看>>
service层自定义异常事物回滚
查看>>
NodeJS——模块全局安装路径配置以及关于supervisor的问题解释
查看>>
javascript改变样式(cssFloat,styleFloat)
查看>>
动态嵌套游标解决方案
查看>>
20141230 mysql数值类型和列属性一
查看>>
面试lua笔试题各种坑
查看>>
【Python】Python基础
查看>>
如何在程序中添加iAd广告
查看>>
win32 GDI 画图 防止闪烁
查看>>
Windows 任务管理器中的几个内存概念
查看>>
DNS区域传送、子域授权
查看>>
android
查看>>
360电话面试
查看>>