![](/img/trans.png)
[英]Efficient execution and output stream redirection of process spawned with Runtime.exec()
[英]display the output-stream of a Process returned by Runtime.exec()
如何打印执行命令所获得的输出字符串以stdout?
所以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
。
如将OutputStream转换为String中所述 ,将流转换为字符串 ,只需使用Sysrem.out.print()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.