1// Java program to demonstrate redirection in System.out.println()
2import java.io.*;
3
4public class SystemFact
5{
6 public static void main(String arr[]) throws FileNotFoundException
7 {
8 // Creating a File object that represents the disk file.
9 PrintStream o = new PrintStream(new File("A.txt"));
10
11 // Store current System.out before assigning a new value
12 PrintStream console = System.out;
13
14 // Assign o to output stream
15 System.setOut(o);
16 System.out.println("This will be written to the text file");
17
18 // Use stored value for output stream
19 System.setOut(console);
20 System.out.println("This will be written on the console!");
21 }
22}