国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

解析StreamReader與文件亂碼問題的解決方法

2020-01-24 03:21:37
字體:
來源:轉載
供稿:網友

相信很多人在讀取文件的時候都會碰到亂碼的情況,所謂亂碼就是錯亂的編碼的意思,造成亂碼的是由于編碼不一致導致的。

演示程序:

新建3個文本文件:

clip_image002

編碼和名字一樣,分別是ansi,Unicode,utf8

里面的內容都是:

~@#%……&*()

abcdefg

123456789

測試數據

clip_image004

clip_image006

clip_image008

 

讀取這些文件的代碼如下:

public static void Main()

{

    List<string> lstFilePath = new List<string>()

    {

        "H://TestText//ansi.txt",

        "H://TestText//unicode.txt",

        "H://TestText//utf8.txt"

    };

    foreach (string filePath in lstFilePath)

    {

        using (StreamReader reader = new StreamReader(filePath))

        {

            Console.WriteLine("讀取文件" + filePath);

            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("************************************************************");

        }

    }

}

輸出入下:

clip_image010

 

由于第一個文件使用ansi編碼,但是StreamReader 的默認構造函數使用的是utf8編碼,所以亂碼了。

StreamReader 旨在以一種特定的編碼輸入字符,而 Stream 類用于字節的輸入和輸出。 使用 StreamReader 讀取標準文本文件的各行信息。

除非另外指定, StreamReader 的默認編碼為 UTF-8,而不是當前系統的 ANSI 代碼頁 UTF-8 可以正確處理 Unicode 字符并在操作系統的本地化版本上提供一致的結果。

所以解決上面的編碼問題的解決方案是使用StreamReader,并且傳遞Encoding.Default作為編碼,一般在中文操作系統中,Encoding.Default是Gb2312編碼。

public static void Main()

{

    List<string> lstFilePath = new List<string>()

    {

        "H://TestText//ansi.txt",

        "H://TestText//unicode.txt",

        "H://TestText//utf8.txt"

    };

    foreach (string filePath in lstFilePath)

    {

        using (StreamReader reader = new StreamReader(filePath,Encoding.Default))

        {

            Console.WriteLine("讀取文件" + filePath);

            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("************************************************************");

        }

    }

}

輸出如下:

clip_image012

從這里得到一個結論:使用StreamReader,并且使用Encoding.Default 作為編碼。

很可惜,上面的這個結論在某些情況下頁會存在問題,例如在你的操作系統中Encoding.Default Encoding.UTF8的時候。

最完美的解決方案是:文件使用什么編碼保存的,就用什么編碼來讀取。

那如何得到文件的編碼呢?

使用下面的代碼就可以了:

復制代碼 代碼如下:

public static Encoding GetEncoding(string filePath)
        {
            if (filePath == null)
            {
                throw new ArgumentNullException("filePath");
            }
            Encoding encoding1 = Encoding.Default;
            if (File.Exists(filePath))
            {
                try
                {
                    using (FileStream stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    {
                        if (stream1.Length > 0)
                        {
                            using (StreamReader reader1 = new StreamReader(stream1, true))
                            {
                                char[] chArray1 = new char[1];
                                reader1.Read(chArray1, 0, 1);
                                encoding1 = reader1.CurrentEncoding;
                                reader1.BaseStream.Position = 0;
                                if (encoding1 == Encoding.UTF8)
                                {
                                    byte[] buffer1 = encoding1.GetPreamble();
                                    if (stream1.Length >= buffer1.Length)
                                    {
                                        byte[] buffer2 = new byte[buffer1.Length];
                                        stream1.Read(buffer2, 0, buffer2.Length);
                                        for (int num1 = 0; num1 < buffer2.Length; num1++)
                                        {
                                            if (buffer2[num1] != buffer1[num1])
                                            {
                                                encoding1 = Encoding.Default;
                                                break;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        encoding1 = Encoding.Default;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception exception1)
                {
                    throw;
                }
                if (encoding1 == null)
                {
                    encoding1 = Encoding.UTF8;
                }
            }
            return encoding1;
        }

這段代碼使用encoding1.GetPreamble()方法來得到編碼的字節序列,然后重新讀取數據,比較數據,如果不相同則說明是Encoding.Default.

否則是Encoding.Utf8.

有了GetEncoding(filename)方法后,可以將上面的讀取代碼修改如下:

public static void Main()

{

    List<string> lstFilePath = new List<string>()

    {

        "H://TestText//ansi.txt",

        "H://TestText//unicode.txt",

        "H://TestText//utf8.txt"

    };

    foreach (string filePath in lstFilePath)

    {

        using (StreamReader reader = new StreamReader(filePath, GetEncoding(filePath)))

        {

            Console.WriteLine("讀取文件" + filePath);

            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("當前編碼:" + reader.CurrentEncoding.EncodingName);

            Console.WriteLine("************************************************************");

        }

    }

}

輸出如下:

clip_image002

從這里可以看到ansi 編碼,Encoding.Default 就是簡體中文(GB2312)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 逊克县| 北安市| 忻州市| 安塞县| 夏河县| 泗水县| 岑溪市| 潼关县| 渝北区| 大庆市| 朔州市| 理塘县| 金山区| 莎车县| 六枝特区| 萨迦县| 巫山县| 竹北市| 得荣县| 土默特右旗| 吉林市| 安丘市| 白沙| 靖州| 山丹县| 宣城市| 锡林浩特市| 涡阳县| 图木舒克市| 商河县| 丰镇市| 肃南| 永川市| 蓝田县| 长汀县| 开封市| 治县。| 江陵县| 宜宾县| 芦溪县| 台南县|