简体   繁体   English

TStringList.LoadFromFile Unicode

[英]TStringList.LoadFromFile Unicode

I am attempting to open a txt file to a StringList but if I open a UTF-8 format it fails to load, this is confusing because I have Unicode XE2, am I missing something stupid here? 我试图将一个txt文件打开到StringList但是如果我打开一个UTF-8格式它无法加载,这是令人困惑的,因为我有Unicode XE2,我在这里错过了一些愚蠢的东西吗?

Simple Sample 简单样本

Sl := tStringList.Create;

SL.LoadFromFile(sFilePath);

For i =0 to SL.Count -1 do
  foo

but the String does not load when the txt file is UTF-8 but works fine when its in ANSI format. 但是当txt文件是UTF-8时,String不会加载,但在ANSI格式时它可以正常工作。

TStringList.LoadFromFile will attempt to infer the encoding from the file's byte order mark (BOM). TStringList.LoadFromFile将尝试从文件的字节顺序标记(BOM)推断编码。 If no BOM is present then ANSI encoding is assumed. 如果没有BOM,则假定为ANSI编码。

In your case it seems clear that there is no BOM, so you must tell LoadFromFile which encoding to use. 在您的情况下,似乎很清楚没有BOM,因此您必须告诉LoadFromFile使用哪种编码。 Do that by specifying the encoding as the second argument passed to LoadFromFile : 通过将编码指定为传递给LoadFromFile的第二个参数来执行此操作:

SL.LoadFromFile(sFilePath, TEncoding.UTF8);

If your UTF-8 file does have a BOM, then loading a UTF-8 file which contains an invalid UTF-8 byte sequence will produce an empty result, with no exception or indication of the failure. 如果您的UTF-8文件确实有BOM,则加载包含无效UTF-8字节序列的UTF-8文件将产生空结果,没有异常或指示失败。 This is a 'feature' of the Delphi file handling. 这是Delphi文件处理的“功能”。 So if you see this result and your file has a valid BOM, check the content. 因此,如果您看到此结果且文件包含有效的BOM,请检查内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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