Click here to Skip to main content
15,886,110 members
Articles / General Programming / File
Alternative
Tip/Trick

A quick and easy way to direct Java System.out to File and to Console

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
2 Feb 2012CPOL 8.6K   1  
try{ FileOutputStream fout= new FileOutputStream("stdout.log"); FileOutputStream ferr= new FileOutputStream("stderr.log"); TeeOutputStream multiOut= new TeeOutputStream(System.out, fout); TeeOutputStream multiErr= new TeeOutputStream(System.err, ferr); ...
Java
try
{
    FileOutputStream fout= new FileOutputStream("stdout.log");
    FileOutputStream ferr= new FileOutputStream("stderr.log");
    
    TeeOutputStream multiOut= new TeeOutputStream(System.out, fout);
    TeeOutputStream multiErr= new TeeOutputStream(System.err, ferr);
    
    PrintStream stdout= new PrintStream(multiOut);
    PrintStream stderr= new PrintStream(multiErr);
    
    System.setOut(stdout);
    System.setErr(stderr);
}
catch (FileNotFoundException ex)
{
    //Could not create/open the file
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --