Home Tutorial Java Core Files Java Redirect output to file

 
 

Java Redirect output to file
Posted on: July 20, 2006 at 12:00 AM
This section illustrates you how to redirect the output to the file.

Java Redirect output to file

This section illustrates you how to redirect the output to the file.

It may be comfortable to a programmer if they are able to redirect the run-time exceptions and SOPs (System.out.println) to a file for future reference. Here we are going to discuss about redirecting console outputs to the file.

In the given example, we have set the output stream of System class by creating an instance of PrintStream class with FileOutputStream as argument. This will redirect any console output using System.out.println() to the file specified in the FileOutputStream.

Here is the code:

import java.io.*;

public class RedirectOutputToFile {
	public static void main(String[] argv) throws Exception {
		System.setOut(new PrintStream(new FileOutputStream("C:/output.dat")));
		System.out.println("Hello India");
	}
}

Related Tags for Java Redirect output to file:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.