[英]Delphi - TStringList Save/Load to stream encoding
I'm working with TStringList
with TMemeorySteam
a lot in my project. 我正在我的项目中使用
TStringList
和TMemeorySteam
。
msTmp := TMemoryStream.Create;
try
lTemp.SaveToStream(msTmp, TEncoding.Unicode);
finally
msTmp.Free;
end;
.....
lTemp := TStringList.Create;
try
lTemp.LoadFromFile(msTmp, TEncoding.Unicode);
finally
lTemp.Free;
end;
How i can tell the LoadFromStream()
and SaveToStream()
that the stream is UTF-16 encoded by default in my project without including the encoded type in each call. 我怎么能告诉
LoadFromStream()
和SaveToStream()
默认情况下我的项目中的流是UTF-16编码的,而不包括每次调用中的编码类型。 so i can call LoadFromStream(msTmp)
only and it will load with UTF-16 encoded. 所以我只能调用
LoadFromStream(msTmp)
,它将加载UTF-16编码。
Have a look at the TStrings.DefaultEncoding
property. 看看
TStrings.DefaultEncoding
属性。
The default encoding for the current object.
当前对象的默认编码。
DefaultEncoding is used when the nil encoding is specified in a call to
LoadFromStream
orSaveToStream
.在对
LoadFromStream
或SaveToStream
的调用中指定nil编码时,将使用DefaultEncoding 。By default, DefaultEncoding is set to
Default
.默认情况下, DefaultEncoding设置为
Default
。 The user can change DefaultEncoding if another default encoding is desired forLoadFromStream
orSaveToStream
.如果
LoadFromStream
或SaveToStream
需要另一个默认编码,则用户可以更改DefaultEncoding 。
However, watch out for the TStrings.Encoding
property: 但是,请注意
TStrings.Encoding
属性:
Character encoding determined during reading from a stream or file.
在从流或文件读取期间确定的字符编码。
Encoding
is a read-only property that contains the value of the character encoding detected when theLoadFromStream
orLoadFromFile
methods are called.Encoding
是一个只读属性,包含调用LoadFromStream
或LoadFromFile
方法时检测到的字符编码的值。 If a file or stream does not contain a BOM (the encoding value cannot be detected) thenEncoding
is set to the value specified in theDefaultEncoding
property.如果文件或流不包含BOM(无法检测到编码值),则“
Encoding
将设置为DefaultEncoding
属性中指定的值。
Encoding
is used in theSaveToStream
andSaveToFile
methods.Encoding
用于SaveToStream
和SaveToFile
方法。
If the
Encoding
parameter [ofLoadFromStream
] is not given, then the strings are loaded using the appropriate encoding.如果未给出[
LoadFromStream
]的Encoding
参数,则使用适当的编码加载字符串。 The value of the encoding is obtained by calling theGetBufferEncoding
routine of theTEncoding
class.通过调用
TEncoding
类的GetBufferEncoding
例程来获取编码的值。LoadFromStream
then saves the value of the encoding in theEncoding
property, to be used if the stream is saved.然后,
LoadFromStream
将编码值保存在Encoding
属性中,以便在保存流时使用。
So, as long as you do not call LoadFrom...()
, you can set DefaultEncoding
to TEncoding.Unicode
and then call SaveTo...()
without specifying a value for the Encoding
parameter. 因此,只要不调用
LoadFrom...()
,就可以将DefaultEncoding
设置为TEncoding.Unicode
,然后调用SaveTo...()
而不指定Encoding
参数的值。
But, once you call LoadFrom...()
, the Encoding
property takes priority over the DefaultEncoding
property for subsequent calls to SaveTo...()
. 但是,一旦调用
LoadFrom...()
, Encoding
属性优先于DefaultEncoding
属性,以便后续调用SaveTo...()
。 As long as the files are not BOM'ed, the Encoding
property will match the DefaultEncoding
property. 只要文件没有BOM,
Encoding
属性就会匹配DefaultEncoding
属性。 But if a non-UTF16LE BOM is encountered, all bets are off. 但如果遇到非UTF16LE BOM,则所有投注均已关闭。
To ensure TEncoding.Unicode
is always used, you should continue using the Encoding
parameter of LoadFrom...()
and SaveTo...()
, that is why they exist. 为了确保始终使用
TEncoding.Unicode
,您应该继续使用LoadFrom...()
和SaveTo...()
的Encoding
参数,这就是它们存在的原因。 They take first priority, the (Default)Encoding
properties are fallbacks when input/output encodings are not explicitly stated. 它们具有第一优先级,当未明确声明输入/输出编码时,
(Default)Encoding
属性是回退。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.