Java Code Snippet: Redirect System.out into a string

For 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();

}

Dieser Eintrag wurde veröffentlicht in Software-Development und getagged , , . Bookmarken: Permanent-Link. Kommentieren oder ein Trackback hinterlassen: Trackback-URL.
  • Werbung / Ads

Ihr Kommentar

Ihre E-Mail wird niemals veröffentlicht oder verteilt. Benötigte Felder sind mit * markiert

*
*

Du kannst diese HTML Tags und Attribute verwenden: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">