c#读写文件小结(读写文件)
static void Main(string[] args) {
FileStream fs3 = File.Open("c://test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
//创建文件流对象
string text = "nihao";
byte[] data = Encoding.Default.GetBytes(text);
//转换成byte数组
fs3.Write(data, 0, data.Length);
fs3.Flush();
byte[] buffered = new byte[1024];
int count;
string s = "";
fs3.Seek(0,SeekOrigin.Begin);
// 这是关键因为在写的过程中指针已发生改变
//回到起始位置
while ((count=fs3.Read(buffered,0,1024))!=0)
{
s = s + Encoding.UTF8.GetString(buffered, 0, count);
}
Console.WriteLine(s);
Console.ReadKey();
}