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

Source, Archive

lang/java/console.1478558971.txt.gz · Dernière modification : 2016/11/07 23:49 de root