关于读取txt文件中文乱码问题

在处理文件的过程中,读取txt文件出现中文乱码。这种情况是由于编码字符不一致导致。

public static string ReadFile(string path, string fileName)
        {
            FileStream stream = null;
            StreamReader reader = null;
            StringBuilder v = new StringBuilder();

            try
            {
                stream = new FileStream(path + fileName, FileMode.Open);
                reader = new StreamReader(stream,Encoding.GetEncoding("GB2312"));

                string line = reader.ReadLine();
                do
                {
                    v.Append(line);
                    line = reader.ReadLine();
                }
                while (!string.IsNullOrEmpty(line));
            }
            catch
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                }
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
            }
            return v.ToString();
        }

  转换后可得正确结果

 

posted @ 2017-01-06 15:55  蜗牛不要快  阅读(3430)  评论(0编辑  收藏  举报