Redirecting System.out.println() Output to a File in Java Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 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 K kartik Improve Article Tags : Java Java-I/O java-file-handling Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like