【问题标题】:how to read the current line and its preceding two lines using BufferedReader?如何使用 BufferedReader 读取当前行及其前两行?
【发布时间】:2015-10-08 15:52:54
【问题描述】:

我有一个程序让我们说 x 不断使用 file.write() 向 .txt 文件添加值。

另一方面,我有一个程序会继续从早期的 .txt 文件中读取当前添加的条目。

但我想阅读当前条目以及前两个条目。

以编程方式,这就是我想要的。

for (100 iterations)
{
 prog named x ----> this code will enter a value to a .txt file and that too one entry per line.
 prog named y-----> this code will read the just entered value as well as the previous two entries , so all in all we will read three lines and do some computations based on these three values lets say we will take average.
}

任何帮助将不胜感激。

【问题讨论】:

  • 你真的需要重新读取之前的两个值吗?也许你可以简单地记住它们?
  • 不,我想要问题中描述的方式。

标签: java bufferedreader


【解决方案1】:

你能用一个带有生产者和消费者模块的程序来处理这个问题吗?

线程 X 获取数据并将它们放入消息 FIFO,然后将这些数据写入文件, 线程 y 从消息 FIFO 中获取数据并进行一些计算。

DataSource=>thread X=(message FIFO)=>thread Y

                   ||==>file on disk

我无法想象程序 X 和程序 Y 如何相互协调以确保程序 Y 读取最新数据。也许使用计数器来检查条目。

【讨论】:

  • 是的,我可以使用一个包含所有内容的程序。但这将如何读取前两个值。您能否以编程方式解释一下。
  • 我看到你加了java标签,我可以用C#写这个程序吗?
  • 来吧,试一试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 2013-07-04
  • 2012-12-22
  • 2013-10-30
  • 1970-01-01
  • 2015-05-12
相关资源
最近更新 更多