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.
如将OutputStream转换为String中所述 ,将流转换为字符串 ,只需使用Sysrem.out.print()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.