Outils pour utilisateurs

Outils du site


lang:java:console

Ceci est une ancienne révision du document !


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

lang/java/console.1504977146.txt.gz · Dernière modification : 2017/09/09 19:12 de root