c#读取.txt文件,中文乱码问题
我正在做读取文件的一个小东西,但从文本文件中读出的中文都是乱码,我用的是下列程序,请问该怎么解决
using (StreamReader sr = new StreamReader (file))
{
string text="";
String line;
char[] tempChars;
while ((line = sr.ReadLine()) != null)
{
byte[] msg=Encoding.Unicode.GetBytes(line);
Decoder uniDecoder = Encoding.Unicode.GetDecoder();
int charCount = uniDecoder.GetCharCount(msg, 0, msg.Length);
tempChars = new Char[charCount];
int charsDecodedCount = uniDecoder.GetChars(msg, 0, msg.Length, tempChars, 0);
foreach (Char c in tempChars)
{
Console.Write("[{0}]", c);
}
string temp=new string(tempChars);
System.Diagnostics.Debug.WriteLine(temp);
text+=temp;
}
arrayList.Add(text);
}
using (StreamReader sr = new StreamReader (file))
{
string text="";
String line;
char[] tempChars;
while ((line = sr.ReadLine()) != null)
{
byte[] msg=Encoding.Unicode.GetBytes(line);
Decoder uniDecoder = Encoding.Unicode.GetDecoder();
int charCount = uniDecoder.GetCharCount(msg, 0, msg.Length);
tempChars = new Char[charCount];
int charsDecodedCount = uniDecoder.GetChars(msg, 0, msg.Length, tempChars, 0);
foreach (Char c in tempChars)
{
Console.Write("[{0}]", c);
}
string temp=new string(tempChars);
System.Diagnostics.Debug.WriteLine(temp);
text+=temp;
}
arrayList.Add(text);
}
...全文
相关推荐
解决中文乱码问题
别人的博客 解决C#中文乱码问题 放在这里分享一下
C#读写文本文件操作示例
C#读写文本文件操作示例,可以打开TXT格式的文件,写入TXT文件,可能编码有些问题,在打开中文文件的时候有乱码。
VS2019+GDAL3.5.2 重新编译解决中文乱码详细记录
VS2019+GDAL3.5.2 重新编译,解决读取GDB、MDB、SHP文件中文乱码详细修改记录,及遇到的问题解决记录。 c# 扩展类,全面解决中文乱码的问题。
C#_读取TXT文件程序.doc
C#_读取TXT文件程序.doc C# 读取TXT文件程序 2008-06-11 09:56
读取文本的简单例子
读取文本的简单例子,按键选择文本,避免出现乱码. 使用VS2010打开.



