C#讀取中文文件出現(xiàn)亂碼的解決方法
本文實(shí)例講述了C#讀取中文文件出現(xiàn)亂碼的解決方法。分享給大家供大家參考。具體分析如下:
先來(lái)看這段代碼:
FileStream aFile = new FileStream(SingleFile,FileMode.Open);
StreamReader sr = new StreamReader(aFile,Encoding.GetEncoding("gb2312"),true);
string FileContent = sr.ReadToEnd();
aFile.Close();
ProcessData Pd = new ProcessData();
Pd.ProceData(FileContent);
StreamReader 使用3個(gè)參數(shù) 最后一個(gè)自動(dòng)檢測(cè)utf-8,中文大部分是gb2312,如果不是utf-8,就用gb2312
系統(tǒng)自帶utf 檢測(cè) ,見(jiàn)如下:
private void DetectEncoding()
{
if (this.byteLen >= 2)
{
this._detectEncoding = false;
bool flag = false;
if ((this.byteBuffer[0] == 0xfe) && (this.byteBuffer[1] == 0xff))
{
this.encoding = new UnicodeEncoding(true, true);
this.CompressBuffer(2);
flag = true;
}
else if ((this.byteBuffer[0] == 0xff) && (this.byteBuffer[1] == 0xfe))
{
if (((this.byteLen < 4) || (this.byteBuffer[2] != 0)) || (this.byteBuffer[3] != 0))
{
this.encoding = new UnicodeEncoding(false, true);
this.CompressBuffer(2);
flag = true;
}
else
{
this.encoding = new UTF32Encoding(false, true);
this.CompressBuffer(4);
flag = true;
}
}
else if (((this.byteLen >= 3) && (this.byteBuffer[0] == 0xef)) && ((this.byteBuffer[1] == 0xbb) && (this.byteBuffer[2] == 0xbf)))
{
this.encoding = Encoding.UTF8;
this.CompressBuffer(3);
flag = true;
}
else if ((((this.byteLen >= 4) && (this.byteBuffer[0] == 0)) && ((this.byteBuffer[1] == 0) && (this.byteBuffer[2] == 0xfe))) && (this.byteBuffer[3] == 0xff))
{
this.encoding = new UTF32Encoding(true, true);
this.CompressBuffer(4);
flag = true;
}
else if (this.byteLen == 2)
{
this._detectEncoding = true;
}
if (flag)
{
this.decoder = this.encoding.GetDecoder();
this._maxCharsPerBuffer = this.encoding.GetMaxCharCount(this.byteBuffer.Length);
this.charBuffer = new char[this._maxCharsPerBuffer];
}
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- Unity3d發(fā)布IOS9應(yīng)用時(shí)出現(xiàn)中文亂碼的解決方法
- Java讀寫(xiě)txt文件時(shí)防止中文亂碼問(wèn)題出現(xiàn)的方法介紹
- java 逐行讀取txt文本如何解決中文亂碼
- Android讀取本地json文件的方法(解決顯示亂碼問(wèn)題)
- Java讀取、寫(xiě)入文件如何解決亂碼問(wèn)題
- php使用fgetcsv讀取csv文件出現(xiàn)亂碼的解決方法
- java自動(dòng)根據(jù)文件內(nèi)容的編碼來(lái)讀取避免亂碼
- php 讀取文件亂碼問(wèn)題
- php讀取mysql中文數(shù)據(jù)出現(xiàn)亂碼的解決方法
- iOS讀取txt文件出現(xiàn)中文亂碼的解決方法
相關(guān)文章
C#/VB.NET 自定義PPT動(dòng)畫(huà)路徑的步驟
這篇文章主要介紹了C#/VB.NET 自定義PPT動(dòng)畫(huà)路徑的步驟,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-05-05
使用C#快速搭建一個(gè)在windows運(yùn)行的exe應(yīng)用
這篇文章主要介紹了使用C#快速搭建一個(gè)在windows運(yùn)行的exe應(yīng)用,這是一個(gè)比較舊的內(nèi)容,但是一直都沒(méi)有空寫(xiě),今天花點(diǎn)時(shí)間,把我掌握的C# 分享給初學(xué)的人或者感興趣的人,希望能對(duì)你有一定幫助,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-07-07
C# .net實(shí)現(xiàn)貨幣轉(zhuǎn)換示例
這篇文章主要介紹了C# .net實(shí)現(xiàn)貨幣轉(zhuǎn)換,其中包含了try catch、switch語(yǔ)句的運(yùn)用,對(duì)于C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08
淺析WPF中Binding的數(shù)據(jù)校驗(yàn)和類型轉(zhuǎn)換
在WPF開(kāi)發(fā)中,Binding實(shí)現(xiàn)了數(shù)據(jù)在Source和Target之間的傳遞和流通,那在WPF開(kāi)發(fā)中,如何實(shí)現(xiàn)數(shù)據(jù)的校驗(yàn)和類型轉(zhuǎn)換呢,下面就跟隨小編一起學(xué)習(xí)一下吧2024-03-03
C#控制臺(tái)程序輸出等腰三角形并居中顯示實(shí)例
這篇文章主要介紹了C#控制臺(tái)程序輸出等腰三角形并居中顯示實(shí)例,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-03-03
C#中把日志導(dǎo)出到txt文本的簡(jiǎn)單實(shí)例
這篇文章介紹了C#中把日志導(dǎo)出到txt文本的簡(jiǎn)單實(shí)例,有需要的朋友可以參考一下2013-10-10
解析數(shù)字簽名的substring結(jié)構(gòu)(獲取數(shù)字簽名時(shí)間)
解析數(shù)字簽名的substring結(jié)構(gòu),大家參考使用吧2013-12-12
C#中Invoke和BeginInvoke區(qū)別小結(jié)
有時(shí)候,我們不得不跨線程調(diào)用主界面的控件來(lái)進(jìn)行操作,所以為了方便的解決問(wèn)題,.net為我們提供了Invoke?與beginInvoke,那么Invoke和BeginInvoke區(qū)別在哪,本文就來(lái)詳細(xì)的介紹一下2023-08-08

