What is the diff between the Reader/Writer class hierarchy and the InputStream/OutputStream Class?
The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.
Most of the functionality available for byte streams is also provided for character streams. The methods for character streams generally accept parameters of dsata type char parameters, while byte streams, you guessed it, work with byte data types. The names of the methods in both sets of classes are almost identical except for the suffix, that is, character-stream classes end with the suffix Reader or Writer and byte-stream classes end with the suffix InputStream and OutputStream. For example, to read files using character streams, you would use the java.io.FileReader class; for reading it using byte streams you would use java.io.FileInputStream.
Unless you are working with binary data, such as image and sound files, you should use readers and writers (character streams) to read and write information for the following reasons:
– They can handle any character in the Unicode character set (while the byte streams are limited to ISO-Latin-1 8-bit bytes).
– They are easier to internationalize because they are not dependent upon a specific character encoding.
– They use buffering techniques internally and are therefore potentially much more efficient than byte streams.
Bridging the Gap Between Byte and Character Streams
To bridge the gap between the byte and character stream classes, JDK 1.1 and JDK 1.2 provide the java.io.InputStreamReader and java.io.OutputStreamWriter classes. The only purpose of these classes is to convert byte data into character-based data according to a specified (or the platform default) encoding. For example, the static data member “in” in the “System” class is essentially a handle to the Standard Input (stdin) device. If you want to wrap this inside the java.io.BufferedReader class that works with character-streams, you use InputStreamReader class as follows:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Recent Comments