Problem description
This exception occurs when the end of a file or stream is accidentally reached during input. This exception is used by the data input stream to indicate that the end of the stream is reached.
Solution
This exception occurs when the end of a file or stream is accidentally reached. This exception is used by the data input stream to indicate that the end of the stream is reached. We recommend that you catch the exception and stop reading the input stream.
Example:
java.io.EOFException
at libcore.io.Streams.readAsciiLine(Streams.java:203)
at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579)
at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827)
at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283)
at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)Sample code
public static void main(String[] args) {
DataInputStream input = null;
try {
//do something
// Read all characters, until an EOFException is thrown.
input = new DataInputStream(new FileInputStream(FILENAME));
while(true) {
char num;
try {
num = input.readChar();
System.out.println("Reading from file: " + num);
}
catch (EOFException ex1) {
break; //EOF reached.
}
catch (IOException ex2) {
System.err.println("An IOException was caught: " + ex2.getMessage());
ex2.printStackTrace();
}
}
}
catch (IOException ex) {
System.err.println("An IOException was caught: " + ex.getMessage());
ex.printStackTrace();
}
finally {
try {
// Close the input stream.
input.close();
}
catch(IOException ex) {
System.err.println("An IOException was caught: " + ex.getMessage());
ex.printStackTrace();
}
}
}