Readers and Writers

   

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.)
Byte Stream Class Character Stream Corresponding Class( Reader / Writer ) Function ( InputStream / OutputStream )
InputStream Reader Abstract class from which all other classes inherit methods , and so on
BufferedInputStream BufferedReader Provides a buffer for input operations
LineNumberInputStream LineNumberReader Keeps track of line numbers
ByteArrayInputStream CharArrayReader Reads from an array
N/A InputStreamReader Translates a byte stream into a character stream
FileInputStream FileReader Allows input from a file on disk
FilterInputStream FilterReader Abstract class for filtered input
PushbackInputStream PushbackReader Allows characters to be pushed back into the stream
PipedInputStream PipedReader Reads from a process pipe
StringBufferInputStream StringReader Reads from a String
OutputStream Writer Abstract class for character-output streams
BufferedOutputStream BufferedWriter Buffers output, uses platform's line separator
ByteArrayOutputStream CharArrayWriter Writes to a character array
FilterOutputStream FilterWriter Abstract class for filtered character output
N/A OutputStreamWriter Translates a character stream into a byte stream
FileOutputStream FileWriter Translates a character stream into a byte file
PrintStream PrintWriter Prints values and objects to a Writer
PipedOutputStream PipedWriter Writes to a PipedReader
N/A StringWriter Writes to a String

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).

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net