Home Tutorial Java Core Files Java Redirect output to file

 
 

Share on Google+Share on Google+
Java Redirect output to file
Posted on: July 20, 2006 at 12:00 AM
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");
	}
}
Advertisement

Related Tags for Java Redirect output to file:


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.

Posted on: July 20, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com