txt文件内有中文和英文FileStream正常;文件中只有纯中文时FileStream写入,打开文本文件时乱码,用FileStream读取正常
代码如下:
数据读取:正常
FileStream Fs = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Read);
byte[] buffer = new byte[1023 * 1024 * 5];
int FS = Fs.Read(buffer, 0, buffer.Length);
Console.WriteLine("实际有效读取字节数:{0}", FS);
string Str = Encoding.UTF8.GetString(buffer, 0, FS);
Fs.Close();
Fs.Dispose();
Console.WriteLine("实际有效读取字符串:\n{0}", Str);
写入数据:文本文件采用UTF8编码保存,文本内容全是中文,写入就会出现乱码,文本内容有中文有英文,写入就不会出现乱码
系统:win10 编程软件:vs2019
string str = @"C:\Users\Administrator\Desktop\test.txt";
using (FileStream Fs1 = new FileStream(str, FileMode.OpenOrCreate, FileAccess.Write))
{
string Str1 = "xxxxx";
byte[] buffer1 = Encoding.UTF8.GetBytes(Str1);
Fs1.Write(buffer1, 0, buffer1.Length);
Console.WriteLine("写入OK!");
}