Tech and Media Labs
This site uses cookies to improve the user experience.

Java IO: Reader

Jakob Jenkov
Last update: 2015-09-04

The Java Reader class ( is the base class for all Reader subclasses in the Java IO API. A Reader is like an InputStream except that it is character based rather than byte based. In other words, a Java Reader is intended for reading text, whereas an InputStream is intended for reading raw bytes.

Characters in Unicode

Today, many applications use UTF (UTF-8 or UTF-16) to store text data. It may take one or more bytes to represent a single character in UTF-8. In UTF-16 each character takes 2 bytes to represent. Therefore, when reading text data, a single byte in the data may not correspond to one character in UTF. If you just read one byte at a time of UTF-8 data via an InputStream and try to convert each byte into a char, you may not end up with the text you expected.

To solve this problem we have the Reader class. The Reader class is capable of decoding bytes into characters. You need to tell the Reader what character set to decode. This is done when you instantiate the Reader (actually, when you instantiate one of its subclasses).

Reading Characters With a Reader

The read() method of a Reader returns an int which contains the char value of the next character read. If the read() method returns -1, there is no more data to read in the Reader, and it can be closed. That is, -1 as int value, not -1 as byte or char value. There is a difference here!

Reader Subclasses

You will normally use a Reader subclass rather than a Reader directly. Java IO contains a lot of Reader subclasses. For instance, the InputStreamReader, CharArrayReader, FileReader, plus many others. For a full overview of Reader subclasses see the Java IO Class Overview Table.

Readers and Sources

A Reader is typically connected to some source of data like a file, char array, network socket etc. This is also explained in more detail in the Java IO Overview text.

Jakob Jenkov

Copyright  Jenkov Aps
Close TOC