我需要将Unix命令"tail -f“包装在一个BufferedInputStream中。我不想模拟或模仿this question所说的尾巴。相反,我想使用tail,等待它给我一个新的行。
发布于 2009-06-12 18:55:08
最好的方法是使用Process类并使用Scanner读取
Runtime r = Runtime.getRuntime()
Process p = r.exec("tail -f")
Scanner s = new Scanner(p.getInputStream())
while (s.hasNextLine()) {
String line = s.nextLine()
// Do whatever you want with the output.
}hasNextLine()应该会阻塞,因为它正在等待来自输入流的更多输入,这样您就不会忙于等待数据的到来。
发布于 2009-06-12 18:47:17
看看Runtime.exec(字符串命令)。返回具有输入流和输出流的Process对象。
发布于 2009-06-12 19:44:26
另请检查ProcessBuilder
Process tail = new ProcessBuilder("tail", "-f", file).start();
BufferedInputStream bis = new BufferedInputStream(tail.getInputStream())其中,日志是类似“/var/ file /messages”的字符串。
https://stackoverflow.com/questions/988327
复制相似问题