我有一个文件,其中包含我想监视更改以及添加我自己的更改的数据。像“Tail -f foo.txt”一样思考。
基于这个线程,看起来我应该只创建一个文件流,并将其同时传递给写入器和读取器。但是,当阅读器到达原始文件的末尾时,它看不到我自己编写的更新。
我知道这似乎是一个奇怪的情况......它更像是一个实验,看看它是否可以完成。
这是我尝试过的示例:
foo.txt:
a
b
c
d
e
f
string test = "foo.txt";
System.IO.FileStream fs = new System.IO.FileStream(test, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
var sw = new System.IO.StreamWriter(fs);
var sr = new System.IO.StreamReader(fs);
var res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("g");
sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
sw.WriteLine("h");
sw.Flush();
sw.WriteLine("i");
sw.Flush();
sw.WriteLine("j");
sw.Flush();
sw.WriteLine("k");
sw.Flush();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
res = sr.ReadLine();
超过“f”后,阅读器返回 null。