I'm trying to intercept System.out and System.err, but maintain the ability to write to the original streams directly when necessary.
PrintStream ps = System.out; System.setOut(new MyMagicPrintStream()); ps.println("foo");
Unfortunately, the details of the System class' implementation means that in my example, "foo" gets sent to MyMagicPrintStream
instead of the real stdout
.
Does anyone know how to get references to the real/original OutputStreams
?
Thanks.
PS: This will otherwise result in a StackOverflowError <-- for SEO.
The static System. setOut() method is used to reassign the standard output stream. This method first uses the checkPermission() method to check for the security manager and its permissions.
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently.
try this:
PrintStream ps = new PrintStream(new FileOutputStream(FileDescriptor.out))
Try something like this :
PrintStream original = new PrintStream(System.out); // replace the System.out, here I redirect to NUL System.setOut(new PrintStream(new FileOutputStream("NUL:"))); System.out.println("bar"); // no output // the original stream is still available original.println("foo"); // output to stdout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With