static void Main(string[] args)
{
//创建文件
using (FileStream fsWrite = new FileStream(@”C:\Users\pqx\Desktop\new.txt”, FileMode.OpenOrCreate, FileAccess.Write))
{
string str = “这是要写进文本的内容:行百里者半九十”;
//将上述字符串转换成字节数组
byte[] buffer = Encoding.Default.GetBytes(str);
//写入文本
fsWrite.Write(buffer, 0, buffer.Length);
Console.WriteLine(“写入成功”);
}
//读取文本内容
using (FileStream fsRead = new FileStream(@”C:\Users\pqx\Desktop\new.txt”, FileMode.OpenOrCreate, FileAccess.Read))
{
//定义一个字节缓冲区,大小为2M
byte[] buffer1 = new byte[1024 * 1024 * 2];
int r = fsRead.Read(buffer1, 0, buffer1.Length);
//将字节数组中的每一个元素按照指定的编码格式解码成字符串
string s = Encoding.Default.GetString(buffer1, 0, r);
Console.WriteLine(s);
}
Console.ReadKey();
}
最新评论