具有Unicode数据的LoadFromFile [英] LoadFromFile with Unicode data
问题描述
我的输入文件(f)具有一些无法正确读取的Unicode(瑞典语)。
My input file(f) has some Unicode (Swedish) that isn't being read correctly.
这两种方法都不起作用,尽管它们给出的结果不同:
Neither of these approaches works, although they give different results:
LoadFromFile(f);
或
LoadFromFile(f,TEncoding.GetEncoding(GetOEMCP));
我正在使用Delphi XE
I'm using Delphi XE
我怎样才能LoadFromFile一些Unicode数据...。又如何随后保存到文件?谢谢
How can I LoadFromFile some Unicode data....also how do I subsequently SaveToFile? Thanks
推荐答案
要加载Unicode文本文件,您需要知道其编码。如果文件具有字节顺序标记(BOM),则只需调用 LoadFromFile(FileName)
,RTL将使用BOM来确定编码。
In order to load a Unicode text file you need to know its encoding. If the file has a Byte Order Mark (BOM), then you can simply call LoadFromFile(FileName)
and the RTL will use the BOM to determine the encoding.
如果文件没有BOM,则需要明确指定编码,例如
If the file does not have a BOM then you need to explicitly specify the encoding, e.g.
LoadFromFile(FileName, TEncoding.UTF8);
LoadFromFile(FileName, TEncoding.Unicode);//UTF-16 LE
LoadFromFile(FileName, TEncoding.BigEndianUnicode);//UTF-16 BE
出于某种原因,我不知道,没有对UTF-32的内置支持,但是如果您有这样的文件,那么就很容易添加 TEncoding
实例来处理该问题。
For some reason, unknown to me, there is no built in support for UTF-32, but if you had such a file then it would be easy enough to add a TEncoding
instance to handle that.
这篇关于具有Unicode数据的LoadFromFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!