Tony R Tony R - 3 months ago 43
Java Question

Java Process getInputStream vs. getOutputStream

I'm a bit confused about the streams... which is which?

Simply, which stream should I use to catch the output of my Process, and which stream should I use to give my Process some input?

Answer

You can only read from an InputStream, so use that to catch the output of your process.

You write to an OutputStream, so use that to give the process your input.

You are using names that make sense in the context of the spawned process. But the API names make sense in the context of the parent process.

Here's another tip: if your process writes to standard error, be sure to read that too. If standard output or error pipes of the sub-process are full (because your parent Java process isn't consuming them), the child process will block on its write() calls.