在處理文件的過程中,讀取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();
}
轉換后可得正確結果
