当我们用System.IO.StreamReader读取包含汉字的txt文件时,经常会读出乱码(StreamWriater写文本文件也有类似的问题),原因很简单,就是文件的编码(encoding)和StreamReader/Writer的encoding不对应。
为了解决这个问题,我写了一个类,来取得一个文本文件的encoding,这样我们就可以创建对应的StreamReader和StreamWriter来读写,保证不会出现乱码现象。其实原理很简单,文本编辑器(比如XP自带的记事本)在生成文本文件时,如果编码格式和系统默认的编码(中文系统下默认为GB2312)不一致时,会在txt文件开头部分添加特定的“编码字节序标识(Encoding Bit Order Madk,简写为BOM)”,类似PE格式的"MZ"文件头。这样它在读取时就可以根据这个BOM来确定该文本文件生成时所使用的Encoding。这个BOM我们用记事本等程序打开默认是看不到的,但是用stream按字节读取时是可以读到的。我的这个TxtFileEncoding类就是根据这个BOM“文件头”来确定txt文件生成时用到的编码的。
using System; using System.IO; using System.Text; namespace XXX.XXX.XXX.Common { public class EncodingHelper { /// <summary> /// GetEncoding /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static Encoding GetEncoding(string fileName) { return GetEncoding(fileName, Encoding.Default); } /// <summary> /// GetEncoding /// </summary> /// <param name="stream"></param> /// <returns></returns> public static Encoding GetEncoding(FileStream stream) { return GetEncoding(stream, Encoding.Default); } /// <summary> /// GetEncoding /// </summary> /// <param name="fileName"></param> /// <param name="defaultEncoding"></param> /// <returns></returns> public static Encoding GetEncoding(string fileName, Encoding defaultEncoding) { var fs = new FileStream(fileName, FileMode.Open); var targetEncoding = GetEncoding(fs, defaultEncoding); fs.Close(); return targetEncoding; } /// <summary> /// GetEncoding /// </summary> /// <param name="stream"></param> /// <param name="defaultEncoding"></param> /// <returns></returns> public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding) { var targetEncoding = defaultEncoding; if (stream != null && stream.Length >= 2) { //保存文件流的前4个字节 byte byte1 = 0; byte byte2 = 0; byte byte3 = 0; byte byte4 = 0; //保存当前Seek位置 long origPos = stream.Seek(0, SeekOrigin.Begin); stream.Seek(0, SeekOrigin.Begin); int nByte = stream.ReadByte(); byte1 = Convert.ToByte(nByte); byte2 = Convert.ToByte(stream.ReadByte()); if (stream.Length >= 3) { byte3 = Convert.ToByte(stream.ReadByte()); } if (stream.Length >= 4) { byte4 = Convert.ToByte(stream.ReadByte()); } //根据文件流的前4个字节判断Encoding //Unicode {0xFF, 0xFE}; //BE-Unicode {0xFE, 0xFF}; //UTF8 = {0xEF, 0xBB, 0xBF}; if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe { targetEncoding = Encoding.BigEndianUnicode; } if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode { targetEncoding = Encoding.Unicode; } if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8 { targetEncoding = Encoding.UTF8; } //恢复Seek位置 stream.Seek(origPos, SeekOrigin.Begin); } return targetEncoding; } /// <summary> /// GetEncoding /// </summary> /// <param name="bytes"></param> /// <param name="defaultEncoding"></param> /// <returns></returns> private static Encoding GetEncoding(byte[] bytes, Encoding defaultEncoding) { var targetEncoding = defaultEncoding; byte byte1 = 0; byte byte2 = 0; byte byte3 = 0; if (bytes.Length >= 1) { byte1 = bytes[0]; } if (bytes.Length >= 2) { byte2 = bytes[1]; } if (bytes.Length >= 3) { byte3 = bytes[2]; } if (byte1 == 0xFE && byte2 == 0xFF) { targetEncoding = Encoding.BigEndianUnicode; } if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF) { targetEncoding = Encoding.Unicode; } if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) { targetEncoding = Encoding.UTF8; } return targetEncoding; } } }