import java.io.*;
//=========================================================================
// Redirect System.out and System.err to files
//=========================================================================
public class SystemRedirectOut 
{
    public static void main(String[] args) 
    {
        System.out.println("This text should go to the screen as part of 'System.out'");
        System.err.println("This text should go to the screen as part or 'System.err'");

        String filenameOut = "SystemRedirectOut.txt";
        String filenameErr = "SystemRedirectErr.txt";
//      String filenameOut = "/home/sultans/web/java/demo/util/SystemRedirectOut.txt";
//      String filenameErr = "/home/sultans/web/java/demo/util/SystemRedirectErr.txt";

        try {
            FileOutputStream outStream = new FileOutputStream(filenameOut, true);   //append
            PrintStream      output    = new PrintStream(outStream);
            FileOutputStream errStream = new FileOutputStream(filenameErr, true);   //append
            PrintStream      error     = new PrintStream(errStream);

            System.setOut(output);              //redirect System.out to a file
            System.setErr(error);               //redirect System.err to a file

            System.out.println("This text should go to the 'out' file as part of 'System.out'");
            System.err.println("This text should go to the 'error' file as part or 'System.err'");
 
            outStream.close();
            output.close();
            errStream.close();
            error.close();
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
            System.exit(-1);
        }
    }
}