简体   繁体   English

Delphi - TStringList保存/加载流编码

[英]Delphi - TStringList Save/Load to stream encoding

I'm working with TStringList with TMemeorySteam a lot in my project. 我正在我的项目中使用TStringListTMemeorySteam

  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 or SaveToStream . 在对LoadFromStreamSaveToStream的调用中指定nil编码时,将使用DefaultEncoding

By default, DefaultEncoding is set to Default . 默认情况下, DefaultEncoding设置为Default The user can change DefaultEncoding if another default encoding is desired for LoadFromStream or SaveToStream . 如果LoadFromStreamSaveToStream需要另一个默认编码,则用户可以更改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 the LoadFromStream or LoadFromFile methods are called. Encoding是一个只读属性,包含调用LoadFromStreamLoadFromFile方法时检测到的字符编码的值。 If a file or stream does not contain a BOM (the encoding value cannot be detected) then Encoding is set to the value specified in the DefaultEncoding property. 如果文件或流不包含BOM(无法检测到编码值),则“ Encoding将设置为DefaultEncoding属性中指定的值。

Encoding is used in the SaveToStream and SaveToFile methods. Encoding用于SaveToStreamSaveToFile方法。

If the Encoding parameter [of LoadFromStream ] is not given, then the strings are loaded using the appropriate encoding. 如果未给出[ LoadFromStream ]的Encoding参数,则使用适当的编码加载字符串。 The value of the encoding is obtained by calling the GetBufferEncoding routine of the TEncoding class. 通过调用TEncoding类的GetBufferEncoding例程来获取编码的值。 LoadFromStream then saves the value of the encoding in the Encoding 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM