Pages

Tuesday, August 2, 2011

Code Snippet: Redirecting Standard Output to File

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;


public class SystemOutRedirect {

  public static void main(String[] args) throws FileNotFoundException {
    System.setOut(new PrintStream(new FileOutputStream("stdout.txt")));
    System.setErr(new PrintStream(new FileOutputStream("error.txt")));

    System.out.println("hello");  //goes to stdout.txt
    System.err.println("error");  //goes to error.txt
  }

}

No comments:

Disqus for Code Impossible