Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / programming / file

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

0.00/5 (No votes)
2 Feb 2012CPOL 8.8K  
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)