Outils pour utilisateurs

Outils du site


lang:java:console

Rediriger la sortie standard vers une variable String

// Create a stream to hold the output
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
// IMPORTANT: Save the old System.out!
PrintStream old = System.out;
// Tell Java to use your special stream
System.setOut(ps);
// Print some output: goes to your special stream
System.out.println("Foofoofoo!");
// Put things back
System.out.flush();
System.setOut(old);
// Show what happened
System.out.println("Here: " + baos.toString());

Redirect console output to string in java - Stack Overflow Archive du 03/01/2012 le 29/04/2020

lang/java/console.txt · Dernière modification : 2020/04/29 22:18 de root