Thursday, June 25, 2009

Redirecting the output of System.out.println, System.err.println to a file/log file.

Sometimes you might need to redirect the output System.out.println,System.err.println to a log file.Refer the following code snippet for doing this

public static void main(String[] args) throws Exception {
ABC abc = new ABC();
PrintStream printStream;
printStream = new PrintStream(new FileOutputStream("C://javaout.log"));
System.setOut(printStream);
System.setErr(printStream);
System.out.println("I am from Out...");
System.err.println("I am from Error");
}

Now check the javaout.log in c drive

Redirecting exception' printStackTrace to a file
catch(Exception e){
: : :
e.printStackTrace( new PrintStream(("C://javaout.log")));
: : :



No comments: