views:

51

answers:

3

Hello,

I'm trying to read in from two different input streams (stdin and stderr of a child application) and print them in one combine frame. I've never done this before, and am wondering about best practices.

I can spawn two threads and let them both block on the input stream. Is there a better way?

Thanks!

A: 

I think your best strategy would be using classes in java.nio package. Check this example out.

Pablo Santa Cruz
Impossible. He is using the streams of a ProCess. You can't use NIO on those.
EJP
+1  A: 

Your approach is fine, though if you are trying to combine two streams you may get issues with the output/error getting jumbled together.

Perhaps check out Apache commons-exec which is a decent library for handling process invocation and output/error capture. Under the hood it is doing exactly as you suggest, forking threads which read InputStream(s).

Mike Q
A: 

having two threads is the only solution.

irreputable
No it isn't. See above.
EJP