我在我的h:盘下建立了一个文本文档 并在里面输入了几个汉字:“看看编码”
我存为了.txt格式 应该是ansi编码吧
后来我想看乱码 就建立了一个控制台小程序
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace 编码
{
class Program
{
static void Main(string[] args)
{
string a = File.ReadAllText(@"H:\encoding.txt",Encoding.UTF8);
string b = File.ReadAllText(@"H:\encoding.txt", Encoding.UTF7);
string c= File.ReadAllText(@"H:\encoding.txt",Encoding.GetEncoding("GB2312"));
Console.Write(a);
Console.Write(b);
Console.Write(c);
}
}
}
都是正常显示 没有出现乱码 这事为啥啊
我存为了.txt格式 应该是ansi编码吧
后来我想看乱码 就建立了一个控制台小程序
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace 编码
{
class Program
{
static void Main(string[] args)
{
string a = File.ReadAllText(@"H:\encoding.txt",Encoding.UTF8);
string b = File.ReadAllText(@"H:\encoding.txt", Encoding.UTF7);
string c= File.ReadAllText(@"H:\encoding.txt",Encoding.GetEncoding("GB2312"));
Console.Write(a);
Console.Write(b);
Console.Write(c);
}
}
}
都是正常显示 没有出现乱码 这事为啥啊
ReadAllText (String, Encoding) 打开一个文件,使用指定的编码读取文件的所有行,然后关闭该文件。 编码之间是可以转换的。只是所占的字节数不一样
Note that detectEncodingFromByteOrderMarks is a very
loose attempt at detecting the encoding by looking at the first
3 bytes of the stream. It will recognize UTF-8, little endian
unicode, and big endian unicode text, but that's it. If neither
of those three match, it will use the Encoding you provided.
loose attempt at detecting the encoding by looking at the first
3 bytes of the stream. It will recognize UTF-8, little endian
unicode, and big endian unicode text, but that's it. If neither
of those three match, it will use the Encoding you provided.
一般默认的就是UTF8吧,StreamReader只认识UTF8,小端模式,unicode,你用三种之外的 又不指定正确的编码方式估计解析就有问题了,楼主可以试下