在下面的 while 循环中,我只想读取 Process p 输出中的最新行,忽略循环 hibernate 时进入缓冲区的任何其他内容。我该怎么做?
String s;
Runtime r = Runtime.getRuntime();
Process p = r.exec("SomeContinuousProgram");
myInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (true){
if ((s = myInput.readLine()) != null) {
System.out.println(s);
}
Thread.sleep(sleep);
}
最佳答案
您无法“跳”到进程中写入的最新行。您必须阅读它之前的所有行。
将程序分成 2 个线程。主线程将从 BufferedReader 中读取数据并跟踪最新的行。另一个线程将 hibernate ,然后显示最新行。
关于java Process,getInputStream,仅读取最新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15993591/