由Runtime.exec返回的进程的流()

如何打印输出字符串以执行命令?

因此, Runtime.getRuntime().exec()会返回一个Process ,并且通过调用getOutputStream() ,我可以获得如下的对象,但是如何将它的内容显示到stdout?

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

谢谢


我相信你正在尝试从进程获取输出,为此你应该得到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);

当您想要将输出写入/发送给Process时,您将获得OutputStream


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

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

上一篇: stream of a Process returned by Runtime.exec()

下一篇: Test java programs that read from stdin and write to stdout