George Irimiciuc
George Irimiciuc

Reputation: 4633

Redirecting System.setout back to standard output

System.setout redirects the standard output. For instance,

FileOutputStream f = new FileOutputStream("file.txt");
    System.setOut(new PrintStream(f));

Every System.out.print will write in the file. My question is, how to set the output back to the standard once this has been done?

Upvotes: 4

Views: 1668

Answers (1)

fge
fge

Reputation: 121750

Just save it and restore it:

final PrintStream oldStdout = System.out;
System.setOut(newStdout);
// ...
System.setOut(oldStdout);

Upvotes: 8

Related Questions