7.6 Readers and Writers 

Previous Index Next 


Version 1.1 of the Java Development Kit introduced support for character streams to the java.io package. Prior to this the standard I/O facilities supported only byte streams, via the InputStream and OutputStream classes and their subclasses.

Character streams are like byte streams, but they contain 16-bit Unicode characters rather than eight-bit bytes. They are implemented by the Reader and Writer classes and their subclasses.

Readers and Writers support essentially the same operations as InputStreams and OutputStreams, except that where byte-stream methods operate on bytes or byte arrays, character-stream methods operate on characters, character arrays, or strings. Most of the functionality available for byte streams is also provided for character streams.

This is reflected in the name of each character-stream class, whose prefix is usually shared with the name of the corresponding byte-stream class. For example, there is a PushbackReader class that provides the same functionality for character streams that is provided by PushbackInputStream for byte streams.

Why use character streams?

The primary advantage of character streams is that they make it easy to write programs that are not dependent upon a specific character encoding, and are therefore easy to internationalize.

Java stores strings in Unicode, an international standard character encoding that is capable of representing most of the world's written languages. Typical user-readable text files, however, use encodings that are not necessarily related to Unicode, or even to ASCII, and there are many such encodings.

Character streams hide the complexity of dealing with these encodings by providing two classes that serve as bridges between byte streams and character streams. The InputStreamReader class implements a character-input stream that reads bytes from a byte-input stream and converts them to characters according to a specified encoding. Similarly, the OutputStreamWriter class implements a character-output stream that converts characters into bytes according a specified encoding and writes them to a byte-output stream.

A second advantage of character streams is that they are potentially much more efficient than byte streams. The implementations of many of Java's original byte streams are oriented around byte-at-a-time read and write operations. The character-stream classes, in contrast, are oriented around buffer-at-a-time read and write operations. This difference, in combination with a more efficient locking scheme, allows the character stream classes to make up for the added overhead of encoding conversion in many cases.

How to use a Reader

The following code following code wraps a BufferedReader class around a FileInputStream. This allows the contents of the file to be treated correctly as characters.
FileInputStream inputFile = new FileInputStream("test.txt");
BufferedReader input  = new BufferedReader( new InputStreamReader( inputFile)  );
The BufferedReader class provides methods for reading characters from a chracter stream and a readLine() method that reads a line from the chracter stream and returns it as a string.

The above code can be simplied by using the FileReader class:

BufferedReader input  = new BufferedReader( new FileReader("test.txt") );


Source: