Java“尾-f"包装纸 [英] Java "tail -f" wrapper

查看:24
本文介绍了Java“尾-f"包装纸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 Unix 命令tail -f"包装在 BufferedInputStream 中.我不想模拟或模仿 this question.相反,我想使用 tail,等待它给我换行.

I need to wrap the Unix command "tail -f" in a BufferedInputStream. I don't want to simulate or mimic tail as stated by this question. Rather, I want to use tail, waiting for it to give me a new line.

推荐答案

最好的办法是使用 Process 类并用 Scanner 阅读:

Your best bet is to use the Process class and read with a 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() 应该阻塞,因为它正在等待来自输入流的更多输入,因此您不会因为数据进入而忙于等待.

hasNextLine() should block as it's waiting for more input from the input stream, so you will not be busy-waiting as data comes in.

这篇关于Java“尾-f"包装纸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆