Pages

Tuesday, December 21, 2010

How to redirect the IO Streams

In Java IO streams are flows of data you can either read from, or write to. Streams are typically connected to a data source, or data destination, like a file, network connection etc.

A stream has no concept of an index of the read or written data, like an array does. Nor can you typically move forth and back in a stream, like you can in an array, or in a file using RandomAccessFile. A stream is just a continuous flow of data.

In Java IO streams are typically byte based. This means that you can either read bytes from, or write bytes to a stream. If you need to read / write characters (like Latin1 or UNICODE characters), you should use a Reader or Writer.
OutputStream
The class java.io.OutputStream is the base class of all Java IO output streams. If you are writing a component that needs to write output to a stream, try to make sure that component depends on an OutputStream and not one of its subclasses.

System.out is a static variable of the System class that holds a reference to a PrintStream used to channel output to the host system's console. System.err is also a PrintStream and System.in is an InputStream. When the Java runtime system starts up it automatically hooks-up these object references to the host system's output, error and input streams.

The PrintStream and InputStream types used to interface with the host system are not subclasses of the System class, they are a subclasses of the java.io.OutputStream and InputStream hierarchy. Notably the PrintStream class never throws IOException, though you can use the checkError() method to discover any problems.

The common technique of making print calls with System.out.println() gives the impression that println() may be a method of the System class but it is not, it is a method of the PrintStream class.
To Disable the System.out.println writes in console while in production environments:
throwable object How to redirect the IO Streams
the following will disable the error/exception prints in console:
throwable object How to redirect the IO Streams
To Enable the System.out.println writes in console while in development environments:
throwable object How to redirect the IO Streams

No comments:

Post a Comment