Share on Google+Share on Google+

Java Redirect output to file

Advertisement
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");
	}
}

Advertisements

Posted on: July 20, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.