How to Capture System.err and System.out
Join the DZone community and get the full member experience.
Join For FreeSometimes you need to capture the text being written to a PrintStream by a 3rd party component. Here it is a simple solution for out and err PrintStreams:
//System.out
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
System.setOut(new PrintStream(pipeOut));
// now read from pipeIn
//System.err
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
System.setErr(new PrintStream(pipeOut));
// now read from pipeIn
From http://e-blog-java.blogspot.com/2011/03/how-to-capture-systemout-and-systemerr.html
Opinions expressed by DZone contributors are their own.
Comments