Thursday, May 11, 2006

IO Redirection in Java

A Casual Google search led me to this interesting Java TechTips which talks about redirecting IO to a file as we normally do on command prompt

java test >myfile

Same thing can be achieved by using PrintWriter cleverly.

System class has methods to let you set your own PrintWriter to it. - System.setOut()

As a point of interest, the JDK 1.3 implementation of java.lang.System has internal code of this form:

    FileOutputStream fdOut =
new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(
new BufferedOutputStream(fdOut, 128), true));

Check out this TechTips for more info about IO Redirection and Array Manipulation.