在处理文件的过程中,读取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();
}
转换后可得正确结果