C# 判讀取得字符編碼格式

            FileStream fs1 = new FileStream(folder + strPath, FileMode.Open);

            byte[] bytes = new byte[fs1.Length];
            fs1.Read(bytes, 0, bytes.Length);
            // 設置當前流的位置爲流的開始   
            fs1.Seek(0, SeekOrigin.Begin);

            UniversalDetector Det = new UniversalDetector(null);
            Det.HandleData(bytes, 0, bytes.Length);
            Det.DataEnd();
            //獲得文檔字符編碼類型
            string cLX = Det.GetDetectedCharset();
       string Text = Encoding.GetEncoding(cLX).GetString(bytes);
       //第2種方式 StreamReader sr = new StreamReader(fs1, Encoding.GetEncoding(cLX)); string str1 = sr.ReadToEnd();

 UniversalDetector這個是個第三方的字符編碼識別,準確率仍是能夠的編碼

http://pan.baidu.com/s/1pJ5C6TX   blog

UniversalDetector 源碼連接如上文檔

相關文章
相關標籤/搜索