Home

Noppanit

01 Mar 2011

How to : Java capture output from console

Well, the topic pretty says everything. So, here’s the solution.

final ByteArrayOutputStream myOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(myOut));

// test stuff here...

final String standardOutput = myOut.toString();

The idea is just redirect the System.out to another PrintStream and you can use information in the PrintStream.

Til next time,
noppanit at 00:00

scribble