0

我逐行阅读文本文件,到目前为止一切顺利。我只是用这个:

    using (StreamReader sr = new StreamReader(this._inFilePath))

            {
                string line;
                int index = 0;

                // Read and display lines from the file until the end of 
                // the file is reached:
                while ((line = sr.ReadLine()) != null)
                {
                    //skip empty lines
                    if (line == "")
                    {
                        continue;
                    } 
}
}

现在我注意到我可能需要在阅读文件后将其转换为 Unicode。它是如何完成的?有人使用 Convert 类吗?

4

2 回答 2

0

阅读后无法将文本转换为 Unicode ,因为到那时它已经是一个字符串,包含映射到 Unicode 代码点的实际字符。您在代码示例中所做的是将文件读取Unicode,因为这是默认StreamReader行为

是什么让你认为你必须转换任何东西?文本是否损坏?

于 2009-03-12T22:16:05.970 回答
0

问题解决了:

            using (StreamReader sr = new StreamReader(this._inFilePath, System.Text.Encoding.Default))

显然默认对应于 ANSI,并且生成的文件是 ANSI(不是 UTF-8)。

谢谢大家的回复!

于 2009-03-12T23:43:08.880 回答