Redirect Standard System Output and Error Message to PrintStream in Java

In a Java program, how can you divert standard system output or error messages, say to a file?
->We can achieve this by using
public static void setErr(PrintStream err)
and public static void setOut(PrintStream out) methods.
By default, they both point at the system console. The following example redirects Out and Err messages to 'error.txt'  file

Stream stream = new Stream(new FileOutputStream("error.txt"));
System.setErr(stream);
System.setOut(stream);

You can redirect the Err and Out stream to any PrintStream object.

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...