System IO Redirect : System « Development Class « Java






System IO Redirect

     

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

public class Redirect {
  public static void main(String args[]) throws Exception {
    PrintStream origOut = System.out;
    PrintStream origErr = System.err;

    InputStream stdin = null;
    stdin = new FileInputStream("Redirect.in");
    PrintStream stdout = null;
    stdout = new PrintStream(new FileOutputStream("Redirect.out"));
    PrintStream stderr = null;
    stderr = new PrintStream(new FileOutputStream("Redirect.err"));
    origOut.println("1");
    System.out.println("2");
    origOut.println("3");
    System.err.println("4");
    origErr.println("5");

    System.setIn(stdin);
    System.setOut(stdout);
    System.setErr(stderr);

    origOut.println("\nR");
    System.out.println("T");
    origOut.println("Tq");
    System.err.println("Tqw");
    origErr.println("Test");

    origOut.println("\nRedirect:  Round #3");
    int inChar = 0;
    while (-1 != inChar) {
      try {
        inChar = System.in.read();
      } catch (Exception e) {
        // Clean up the output and bail.
        origOut.print("\n");
        break;
      }
      origOut.write(inChar);
    }

    stdin.close();
    stdout.close();
    stderr.close();

    System.exit(0);
  }
}

   
    
    
    
    
  








Related examples in the same category

1.Terminate a Java application
2.Exiting a Java program
3.Terminate virtual machine using System class
4.Run object finalization using System class
5.Run the garbage collector using System class
6.Reading Text from Standard Input
7.Get file separator using System class
8.System Level Utils
9.Utilities relating to the version of Java in use at runtime
10.Helpers for java.lang.System
11.Methods to aid classes recover from OutOfMemoryErrors by denying or reducing service rather than a complete shutdown of the JVM.
12.A window that displays the bytes sent to System.out and System.err
13.This program runs the Sieve of Erathostenes benchmarkThis program runs the Sieve of Erathostenes benchmark