Java IO: Reader
Reader class is the base class for all
Reader's in the Java IO API.
Reader is like an
InputStream except that it is character based rather
than byte based. In other words, a
Reader is intended for reading text, whereas an
InputStream is intended for reading raw bytes.
Remember, Java uses UTF-8 internally to represent string's. One byte in an input stream may not
correspond to one character in UTF-8. If you just read one byte at a time from an input stream providing
UTF-8 text, and try to convert each byte into a
char you may not end up with the
text you expected.
read() method of a
Reader returns an int which contains the char value of the character read.
read() method returns -1, there is no more data to read in the
and it can be closed. That is, -1 as int value, not -1 as byte or char value. There is a difference here!
You will normally use a
Reader subclass rather than a
FileReader, plus many others. For a full overview of
Reader's see the
Java IO Class Overview Table.
Reader's and Sources
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.