【发布时间】:2011-04-18 13:32:21
【问题描述】:
我有一个文件,其中包含我想要监控更改以及添加我自己的更改的数据。像“Tail -f foo.txt”一样思考。
基于this thread,看起来我应该只创建一个文件流,然后将它同时传递给写入器和读取器。但是,当阅读器到达原始文件的末尾时,它看不到我自己编写的更新。
我知道这似乎是一个奇怪的情况......它更像是一个实验,看看它是否可以完成。
这是我尝试过的示例:
foo.txt:
一个
b
c
d
电子
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”后,reader返回null。
【问题讨论】:
-
张贴者确实提出了一些关于让两个文件流指向同一个对象的内容......确实有效。即使读取器到达文件末尾,如果写入器更新,读取器流也会获得两个结果。
-
是的,那是我删除了我的帖子,因为它没有像我预期的那样工作。我取消删除它并解释了原因...
标签: c# filestream streamreader streamwriter