As a programmer when you are developing an application, you would put various debug statements via System.out.println() statement. But as the program is getting bigger and bigger, these output statements will confuse the program real output on the console (since System.out reference is pointing to the default output stream which is monitor). So, we would need to separate the output debug statements from console without modifying lot of code. You can use below technique If you would want the output to be stored in a separate log file.

PrintlnDemo.java

/**
* A demo class to explain println() output to store in a file
* @author Santhosh Reddy Mandadi
* @since 14-Jun-2012
* @version 1.0
*/

import java.io.*;

public class PrintlnDemo
{
public static void main(String args[]) throws Exception
{
FileOutputStream fis = new FileOutputStream("log.txt");
PrintStream fout = new PrintStream(fis);
PrintStream console = System.out; //This is just to hold the System.out reference
System.out.println("This will go onto the console...");
System.setOut(fout);
System.out.println("This will go onto the file...");
System.out.println("Bye to file...");
System.setOut(console);
System.out.println("Again back to the console");
}
}

System.out references to the default output device. So, we need to change the default reference to file output stream in order to generate the System.out.println() output onto a file. I've taken the default System.out reference and stored it in a local variable called "console", so that we can point that back.