StreamReader 和文件乱码

StreamReader 和文件乱码(转)
原文地址: http://www.cnblogs.com/LoveJenny/archive/2012/02/08/2343297.html




获取文章的编码格式:

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, 
01);
                                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;
        }



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(
"************************************************************");
        }

    }

}




你可能感兴趣的:(StreamReader 和文件乱码)