Readers and Writers
Most programmers have a bad habit of writing code as if all text were ASCII or, at the least, in the native encoding of the platform. While some older, simpler network protocols, such as daytime, quote of the day, and chargen, do specify ASCII encoding for text, this is not true of HTTP and many other more modern protocols, which allow a wide variety of localized encodings, such as K0I8-R Cyrillic, Big-5 Chinese, and ISO 8859-2, for most Central European languages. When the encoding is no longer ASCII, the assumption that bytes and chars are essentially the same things also breaks down. Java’s native character set is the 2-byte Unicode character set. Consequently, Java provides an almost complete mirror of the input and output stream class hierarchy that’s designed for working with characters instead of bytes.
In
this mirror image hierarchy, two abstract superclasses define the
basic API for reading and writing characters. The
java.io.Reader
class specifies the API by which
characters are read. The
java.io.Writer
class specifies the API by which
characters are written. Wherever input and output streams use bytes,
readers and writers use Unicode characters. Concrete subclasses of
Reader and Writer allow
particular sources to be read and targets to be written. Filter
readers and writers can be attached to other readers and writers to
provide additional services or interfaces.
The most important concrete subclasses of Reader
and Writer are the
InputStreamReader ...