Demonstrates standard I/O redirection : IO redirection « File « Java Tutorial






import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class MainClass {
  public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
      System.out.println(s);
    out.close(); 
    System.setOut(console);
  }
}








11.70.IO redirection
11.70.1.Redirect standard error
11.70.2.Redirect standard output
11.70.3.Redirecting Standard Output, and Error
11.70.4.Replace standard output and error with a print stream, output to both the console and to a file.
11.70.5.Standard Err and Output Windows
11.70.6.Redirect standard output to a file
11.70.7.Demonstrates standard I/O redirection