我想同时读取和写入数据到文件中。我可以只对文件使用 StreamReader 和 StreamWriter 吗?为什么下面的代码没有数字?
var stream = new FileStream(path,FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
var sw = new StreamWriter(stream);
var sr = new StreamReader(stream);
for(int i=0;i<10;i++)
{
sw.WriteLine(i);
}
stream.Seek(0,SeekOrigin.Begin);
for(int i=0;i<10;i++)
{
Console.WriteLine(sr.ReadLine());
}
stream.Close();