I recently recognized that you're able to close the PrintStream System.out
!
So the code snippet:
System.out.println("foo");
System.out.close();
System.out.println("bar");
will produce the output:
foo
instead of:
foo
bar
Now to my question:
Is there a way to restore the PrintStream after closing it?
I've seen there's a setOut(PrintStream p)
method in System
which allows me to set the System.out stream to some other stream. But i don't know how to create a stream that's printing to console!
Once again, my question is NOT if i can reopen the System.out
printstream! I'm asking if one could reproduce the steps that java is doing internally to open a PrintStream
that is printing to the console AFTER the System.out
PrintStream was closed!