简体   繁体   中英

How do you pipe an OutputStream and InputStream to console?

When running a process, how do I pipe it's output to System.out and it's input to System.in :

Process p = Runtime.getRuntime().exec("cubc.exe");
// do something with p.getOutputStream())

EDIT: I think I explained this wrong; I don't want to input to the program, I want the user to input to the program, and I don't want to read the output, I want the user to read the output.

Using IOUtils class from Apache Commons IO :

Process p = Runtime.getRuntime().exec("cubc.exe");
IOUtils.copy(p.getInputStream(), System.out);

You can get the input this way:

Scanner scan = new Scanner(p.getInputStream());

Regarding output stream, you can grab it in the same way, and print it using System.out.* methods:

OutputStream os = p.getOutputStream();

You can't really do this with Java, per se. What you can do though is create your process and use PipedOutputStream to catch the output then write it to System.out.println. Other then that, I don't think there is any other way.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM