首先,让我们看看代码:
//The encoding of utf8.txt is UTF-8
StreamReader reader = new StreamReader(@"C:\\utf8.txt", Encoding.UTF8, true);
while (reader.Peek() > 0)
{
//What is the encoding of lineFromTxtFile?
string lineFromTxtFile = reader.ReadLine();
}
正如 Joel 在他著名的 文章 中所说:
如果你有一个字符串,无论是在内存中、文件中还是电子邮件中,你都必须知道它使用的编码方式,否则你将无法正确地解释或显示给用户。
所以我的问题来了:字符串 lineFromTxtFile 的编码是什么?UTF-8(因为它来自一个以 UTF-8 编码的文本文件)?还是 UTF-16(因为 .NET 中的字符串是“Unicode”(UTF-16))?
谢谢。
StreamWriter writer = new StreamWriter(@"C:\unicide.txt",false, Encoding.Unicode);writer.WriteLine(lineFromTxtFile )
? - jjooeell