双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

c#读取.txt文件,中文乱码问题

onsailing onsailing 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我正在做读取文件的一个小东西,但从文本文件中读出的中文都是乱码,我用的是下列程序,请问该怎么解决
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);

}
...全文
评论 6
收藏 已收藏
分享
返回顶部
onsailing
C# 111,129 成员 642,540 内容