Character streams make heavy use of character set converters. They hide the underlying complexity of the conversion process, making it easy for Java programs to be written without knowledge of the internationalizing process. Again, you see that programs are internationalized by default. The advantages of using character streams over byte streams are many. Although character streams have the added overhead of doing character conversion on top of byte reading, they also allow for more efficient buffering. Byte streams are designed to read information one byte at a time, whereas character streams read one buffer at a time. According to Sun, this fact, combined with a new efficient locking scheme, more than compensates for the speed loss caused by the conversion process. Every input or output stream in the old class hierarchy now has a corresponding Reader or Writer class that performs similar functions using character streams (see Table 24.5). Table 24.5. Input/Output Streams And Corresponding Reader And Writer Classes (from Sun Microsystems, Inc.)
The impact of these changes is actually quite minor if you're developing new programs. All you have to do is remember to use Reader and Writer classes where before you used InputStream and OutputStream. The biggest change you'll have to worry about relates to the DataInputStream and PrintStream, which used to be the classes of choice for receiving text from input and sending text to output. The DataInputStream.readLine method has been deprecated ”you should use BufferedReader.readLine instead. Furthermore, you can no longer instantiate a new PrintStream object, although you can still use pre-existing PrintStreams (such as System.out ) for debugging purposes. To output line- terminated strings, you should use the PrintWriter class instead. The main offshoot of this is that all code that is used to communicate with the DataInputStream and PrintStream classes (which includes much Socket, File, and Piped code) will have to be updated to use the proper Reader and Writer classes. To make this easier, Java provides classes called InputStreamReader and OutputStreamWriter, which are used to create a new Writer or Reader based on a byte stream. This makes the Reader / Writer system compatible with all the other classes that currently use byte streams (such as URL, Socket, File, and so on). |