Redirecting System.out.println() Output to a File in Java Last Updated : 10 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report System.out.println() is used mostly to print messages to the console. However very few of us are actually aware of its working mechanism. We can use System.out.println() to print messages to other sources too, not just restricting it to the console. However, before doing so, we must reassign the standard output by using the following method of System class as provided below: Syntax: System.setOut(PrintStream p);Note: PrintStream can be used for character output to a text file. System is a class defined in java.lang package.out is an instance of PrintStream, which is a public and static member of the class System.As all instances of PrintStream class have a public method println(), hence we can invoke the same on out as well. We can assume System.out represents the Standard Output Stream. Procedure: Creation of File class objectInstantiating PrintStream class by passing above object of File class as argument.Calling the out() method of System class by providing PrintStream object.Lastly, print the data using the print() method. The sample input file is as follows: Example: Java // Java Program to Demonstrate Redirection in // System.out.println() By Creating .txt File // and Writing to the file Using // System.out.println() // Importing required classes import java.io.*; // Main class // SystemFact public class GFG { // Main driver method public static void main(String arr[]) throws FileNotFoundException { // Creating a File object that // represents the disk file PrintStream o = new PrintStream(new File("A.txt")); // Store current System.out // before assigning a new value PrintStream console = System.out; // Assign o to output stream // using setOut() method System.setOut(o); // Display message only System.out.println( "This will be written to the text file"); // Use stored value for output stream System.setOut(console); // Display message only System.out.println( "This will be written on the console!"); } } Output: Tip: In a very similar fashion we can use System.out.println() to write to a Socket's OutputStream as well. Comment More infoAdvertise with us Next Article Redirecting System.out.println() Output to a File in Java K kartik Improve Article Tags : Java Java-I/O java-file-handling Practice Tags : Java Similar Reads System.out.println in Java Java System.out.println() is used to print an argument that is passed to it. Parts of System.out.println()The statement can be broken into 3 parts which can be understood separately: System: It is a final class defined in the java.lang package.out: This is an instance of PrintStream type, which is a 5 min read Difference Between System.out.println() and System.err.println() in Java System.out is a PrintStream to which we can write characters. Â It outputs the data we write to it on the Command Line Interface console/terminal. It is mostly used for console applications/programs to display the result to the user. It can be also useful in debugging small programs. Syntax: System.o 2 min read Difference Between System.out.print() and System.out.println() Function in Java In Java, we have the following functions to print anything in the console. System.out.print() andSystem.out.println() But there is a slight difference between both of them, i.e. System.out.println() prints the content and switch to the next line after execution of the statement whereas System.out.pr 2 min read PrintStream println() method in Java with Examples The println() method of PrintStream Class in Java is used to break the line in the stream. This method do not accepts any parameter or return any value. Syntax: public void println() Parameters: This method do not accepts any parameter. Return : This method do not returns any value. Below methods il 2 min read PrintStream println(Object) method in Java with Examples The println(Object) method of PrintStream Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be 2 min read Like