Java"tail -f"包装器

geo*_*wa4 3 java unix tail

我需要在BufferedInputStream中包装Unix命令"tail -f".我不想像这个问题所说的那样模拟或模仿尾巴.相反,我想使用tail,等待它给我一个新的线.

mus*_*pax 14

你最好的选择是使用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.
}
Run Code Online (Sandbox Code Playgroud)

hasNextLine() 应该阻塞,因为它正在等待来自输入流的更多输入,因此您不会忙于等待数据进入.