Output from Process.getInputStream() buffered too long

自古美人都是妖i 提交于 2019-12-24 22:10:04

问题


I have a Java program that uses Process Builder to start an external program.

I have a Process Builder with the command, and I use the code below to get the program going.

What I want to do is to print out the output just as the external program would have done, and I try to do this with the following code:

pb.redirectErrorStream(true);
p = pb.start();
InputStream is = p.getInputStream();
InputStreamReader isr = new inputStreamReader(is);
BufferedReader br = new BufferedReader(isr, 32);
while((line = br.readLine()) != null){
  System.out.println(line);
}

The problem is that my program is all silent until the external program ends, when suddenly all output comes at once. Or it might not have benn silent if there had been more output, but I want it to produce the output as it comes, ie unbuffered.

The (to me) obvious problem is the BufferedReader and the size of its buffer, but the thing is that I've tried to have a really small buffer, to no avail. I've also tried to avoid the BufferedReader, and work with the InputStreamReader directly instead, but thet bahaves the same way.

Anyone here that can understand my problem, and perhaps have a solution?


回答1:


This is caused by buffering in the executed program, not by Java. Nothing you can do about it from the Java end.



来源:https://stackoverflow.com/questions/22832324/output-from-process-getinputstream-buffered-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!