Java Code Snippet: Redirect System.out into a string
19 Januar 2010 | By Christoph in Software-DevelopmentFor a simple test application / prototype I just wanted to just dump everything from System.out into a String, which I can output somewhere e.g. in a JSP file so that I see what’s going on. And yes, I didn’t want to setup a Logging framework for this…I want a String. Here it is:
OutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.out.println(“Test”);
String sysout_content = out.toString();
System.out.println();
}
public static void main(String[] args) {
OutputStream out = new ByteArrayOutputStream();
System.setOut(new PrintStream(out));
System.out.println(“Test”);
String sysout_content = out.toString();
System.out.println();
}