Java Redirect output to file


 

Java Redirect output to file

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

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

Ads