How do I check for end-of-file when reading from a stream?

John Zukowski

Exactly how depends upon which stream you are reading from. If you are reading with the read() method of InputStream/Reader, this will return -1 on EOF. If however you are using BufferedReader.readLine(), then this will return null on end of file. And, the readXXX operations of the DataInput interface throw an EOFException. If you are unclear how to terminate your reading, check the javadoc for the stream you are using.