To redirect or send or save an output into a file

This example demonstrates how you can send or redirect or save your output into a file using java program.

import java.io.*;

class A2{
    public static void main(String[] args) throws IOException{
        FileOutputStream fos=new FileOutputStream(“a2.txt”);
        PrintStream ps=new PrintStream(fos);
        System.setOut(ps);//setErr, setIn
        System.out.println(“Welcome … PS!”);
    }
}