The abstract InputStream.read()
method reads a single byte from an input source and returns its value as an int
in the range 0 to 255. It will return -1 only when the end of the input stream has been reached. The similar Reader.read()
method reads a single character, and returns its value as an int
in the range 0 to 65,535. It also returns -1 only when the end of the stream has been reached. Both methods are meant to be overridden by in subclasses.
These methods are often used to read a byte or character from a stream. Unfortunately, many programmers prematurely convert the resulting int
back to a byte
or char
before checking whether they have reached the end of the stream (indicated by a return value of -1). Programs must check for the end of stream (e.g., -1) before narrowing the return value to a byte
or char
.
...