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("cu开发者_StackOverflow社区bc.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.
精彩评论