获取文本文件的编码,自动区分GB2312和UTF8

下面的是方法  相关代码来自网络

  1 using System; 
  2 using System.IO; 
  3 using System.Text; 
  4 
  5 /// <summary> 
  6 /// FileEncoding 的摘要说明 
  7 /// </summary> 
  8 namespace FileEncoding 
  9 { 
 10 /// <summary> 
 11 /// 获取文件的编码格式 
 12 /// </summary> 
 13 public class EncodingType 
 14 { 
 15 /// <summary> 
 16 /// 给定文件的路径,读取文件的二进制数据,判断文件的编码类型 
 17 /// </summary> 
 18 /// <param name=“FILE_NAME“>文件路径</param> 
 19 /// <returns>文件的编码类型</returns> 
 20 public static System.Text.Encoding GetType(string FILE_NAME) 
 21 { 
 22 FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read); 
 23 Encoding r = GetType(fs); 
 24 fs.Close(); 
 25 return r; 
 26 } 
 27 
 28 /// <summary> 
 29 /// 通过给定的文件流,判断文件的编码类型 
 30 /// </summary> 
 31 /// <param name=“fs“>文件流</param> 
 32 /// <returns>文件的编码类型</returns> 
 33 public static System.Text.Encoding GetType(FileStream fs) 
 34 { 
 35 byte[] Unicode = new byte[] { 0xFF, 0xFE, 0x41 }; 
 36 byte[] UnicodeBIG = new byte[] { 0xFE, 0xFF, 0x00 }; 
 37 byte[] UTF8 = new byte[] { 0xEF, 0xBB, 0xBF }; //带BOM 
 38 Encoding reVal = Encoding.Default; 
 39 
 40 BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default); 
 41 int i; 
 42 int.TryParse(fs.Length.ToString(), out i); 
 43 byte[] ss = r.ReadBytes(i); 
 44 if (IsUTF8Bytes(ss) || (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)) 
 45 { 
 46 reVal = Encoding.UTF8; 
 47 } 
 48 else if (ss[0] == 0xFE && ss[1] == 0xFF && ss[2] == 0x00) 
 49 { 
 50 reVal = Encoding.BigEndianUnicode; 
 51 } 
 52 else if (ss[0] == 0xFF && ss[1] == 0xFE && ss[2] == 0x41) 
 53 { 
 54 reVal = Encoding.Unicode; 
 55 } 
 56 r.Close(); 
 57 return reVal; 
 58 
 59 } 
 60 
 61 /// <summary> 
 62 /// 判断是否是不带 BOM 的 UTF8 格式 
 63 /// </summary> 
 64 /// <param name=“data“></param> 
 65 /// <returns></returns> 
 66 private static bool IsUTF8Bytes(byte[] data) 
 67 { 
 68 int charByteCounter = 1; //计算当前正分析的字符应还有的字节数 
 69 byte curByte; //当前分析的字节. 
 70 for (int i = 0; i < data.Length; i++) 
 71 { 
 72 curByte = data[i]; 
 73 if (charByteCounter == 1) 
 74 { 
 75 if (curByte >= 0x80) 
 76 { 
 77 //判断当前 
 78 while (((curByte <<= 1) & 0x80) != 0) 
 79 { 
 80 charByteCounter++; 
 81 } 
 82 //标记位首位若为非0 则至少以2个1开始 如:110XXXXX...........1111110X 
 83 if (charByteCounter == 1 || charByteCounter > 6) 
 84 { 
 85 return false; 
 86 } 
 87 } 
 88 } 
 89 else 
 90 { 
 91 //若是UTF-8 此时第一位必须为1 
 92 if ((curByte & 0xC0) != 0x80) 
 93 { 
 94 return false; 
 95 } 
 96 charByteCounter--; 
 97 } 
 98 } 
 99 if (charByteCounter > 1) 
100 { 
101 throw new Exception("非预期的byte格式"); 
102 } 
103 return true; 
104 } 
105 
106 } 
107 
108 
109 }
View Code

调用方法

1  using (StreamReader sr = new StreamReader(file.FullName, EncodingType.GetType(file.FullName)))
2                                 {
3                                     string sTemp = string.Empty;
4                                     string sCurEncode = sr.CurrentEncoding.EncodingName;

 

posted @ 2021-05-16 08:28  學海無邊  阅读(143)  评论(0编辑  收藏  举报