
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Redirecting System.out.println() output to a file in Java
In Java, the System is a class that provides access to the resources called "System resources". It is a final class, which means it cannot be inherited by any other class. It is a part of the java.lang package.
The System.out.println() method prints the data on the console. In this article, we will learn how to redirect the output of the System.out.println() method to a file.
Following are few of the ways through we achieve this:
The filed named out of the System class represents a standard output Stream, an object of the PrintStream class.
The println() method of this accepts any a value (of any Java valid type), prints it and terminates the line.
By default, console (screen) is the standard output Stream (System.in) in Java and, whenever we pass any String value to System.out.prinln() method, it prints the given String on the console.
Let's see how we can redirect the output of the System.out.println() method to a file.
Using System.setOut() with PrintStream
The setOut() method of the System class in Java accepts an object of the PrintStream class and makes it the new standard output stream.
Therefore, to redirect the System.out.println() output to a file -
- Create an object of the File class.
- Instantiate a PrintStream class by passing the above-created File object as a parameter.
- Invoke the out() method of the System class, passing the PrintStream object to it.
- Finally, print data using the println() method, and it will be redirected to the file represented by the File object created in the first step.
Example
Following is the Java program to redirect the System.out.println() output to a file using the System.setOut() method:
import java.io.File; import java.io.IOException; import java.io.PrintStream; public class SetOutExample { public static void main(String args[]) throws IOException { //Instantiating the File class File file = new File("D:\sample.txt"); //Instantiating the PrintStream class PrintStream stream = new PrintStream(file); System.out.println("From now on " + file.getAbsolutePath() + " will be your console"); System.setOut(stream); //Printing values to file System.out.println("Hello, how are you"); System.out.println("Welcome to Tutorialspoint"); } }
Output
Following is the output of the above program:
From now on D:\sample.txt will be your console
Using FileOutputStream with PrintStream
The FileOutputStream is a class of the java.io package. It is mainly used for writing data to a file. It creates a file output stream to write data to the specified file.
To redirect the System.out.println() output to a file using the FileOutputStream class, follow the steps below:
- Instantiate the FileOutputStream class by passing the file name as a parameter to its constructor.
- Instantiate the PrintStream class by passing the above-created FileOutputStream object as a parameter.
- Invoke the out() method of the System class, pass the PrintStream object to it.
Example
Following is the Java program to redirect the System.out.println() output to a file using the FileOutputStream class:
import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; public class FileOutputStreamExample { public static void main(String args[]) throws IOException { //Instantiating the FileOutputStream class FileOutputStream fos = new FileOutputStream("D:\sample.txt"); //Instantiating the PrintStream class PrintStream stream = new PrintStream(fos); System.out.println("From now on " + fos.getFD() + " will be your console"); System.setOut(stream); //Printing values to file System.out.println("Hello, how are you"); System.out.println("Welcome to Tutorialspoint"); } }
Output
Following is the output of the above program:
From now on sun.nio.fs.WindowsFileDescriptor@1 will be your console Hello, how are you Welcome to Tutorialspoint