stream of a Process returned by Runtime.exec()

How do I print to stdout the output string obtained executing a command?

So Runtime.getRuntime().exec() would return a Process , and by calling getOutputStream() , I can obtain the object as follows, but how do I display the content of it to stdout?

OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream();

Thanks


I believe you are trying to get the output from process, and for that you should get the InputStream .

InputStream is = Runtime.getRuntime().exec("ls").getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buff = new BufferedReader (isr);

String line;
while((line = buff.readLine()) != null)
    System.out.print(line);

You get the OutputStream when you want to write/ send some output to Process.


将流转换为字符串,如将Get OutputStream转换为字符串所述,并简单地使用Sysrem.out.print()

链接地址: http://www.djcxy.com/p/78450.html

上一篇: Java interop:传递一个OutputStream参数并获取一个String

下一篇: 由Runtime.exec返回的进程的流()