Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 850
George Irimiciuc Avatar asked Oct 16 '25 01:10

George Irimiciuc


1 Answers

Just save it and restore it:

final PrintStream oldStdout = System.out;
System.setOut(newStdout);
// ...
System.setOut(oldStdout);
like image 69
fge Avatar answered Oct 18 '25 14:10

fge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!