IO filestream 将流输入到文件中
代码如下;
View Code
string path = @"c:\temp\wtq_1.txt";
byte[] byteDate=new Byte[1000];
char[] c;
byte[] d;
try
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
string str = "我叫***,为什么我使用这种方式不会乱码";
c = str.ToCharArray();
Console.WriteLine(c.Length);
Console.ReadLine();
foreach (var item in c)
{
Console.Write(item);
}
//byteDate = new byte[c.Length*2];
var Enc = Encoding.UTF8.GetEncoder();
Enc.GetBytes(c, 0, c.Length, byteDate, 0, true);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(byteDate,0,byteDate.Length);
}
}
catch (Exception e)
{
}
byte[] byteDate=new Byte[1000];
char[] c;
byte[] d;
try
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
string str = "我叫***,为什么我使用这种方式不会乱码";
c = str.ToCharArray();
Console.WriteLine(c.Length);
Console.ReadLine();
foreach (var item in c)
{
Console.Write(item);
}
//byteDate = new byte[c.Length*2];
var Enc = Encoding.UTF8.GetEncoder();
Enc.GetBytes(c, 0, c.Length, byteDate, 0, true);
fs.Seek(0, SeekOrigin.Begin);
fs.Write(byteDate,0,byteDate.Length);
}
}
catch (Exception e)
{
}
这样可以把汉字写入到txt中,