Redirect standard error

To redirect standard error (or System.err) there’s a method in the System class called setErr() which takes a PrintStream instance as argument.
If we for example want to redirect all messages on System.err to a file, we can create a FileOutputStream instance and send it to the constructor of the PrintStream.
This Java code example creates the FileOutputStream with the filename “standard_err.txt” and pass it to the PrintStream, which in turn is passed to the setErr() method.
A NullPointerException is forced to test that the stacktrace is printed to the file.

Search for more Java info on this site here:
Custom Search

Please type any questions here.