欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2013年05月13日 12:14:14   作者:  
本篇文章是對StreamReader與文件亂碼問題的解決方法進行了詳細的分析介紹,需要的朋友參考下

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

演示程序:

新建3個文本文件:

clip_image002

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

里面的內(nèi)容都是:

~!@#%……&*()

abcdefg

123456789

測試數(shù)據(jù)

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 的默認構(gòu)造函數(shù)使用的是utf8編碼,所以亂碼了。

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

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

所以解決上面的編碼問題的解決方案是使用StreamReader,并且傳遞Encoding.Default作為編碼,一般在中文操作系統(tǒng)中,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

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

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

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

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

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

復(fù)制代碼 代碼如下:

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()方法來得到編碼的字節(jié)序列,然后重新讀取數(shù)據(jù),比較數(shù)據(jù),如果不相同則說明是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)

相關(guān)文章

最新評論