The java.io.EOFException
is a checked exception in Java that occurs when an end of file or end of stream is reached unexpectedly during input. This exception is mainly used by data input streams to signal end of stream.
Since EOFException
is a checked exception, it must be explicitly handled in methods that can throw this exception - either by using a try-catch block or by throwing it using the throws
clause.
 
What Causes EOFException
While reading the contents of a file or stream using DataInputStream
objects, if the end of the file or stream is reached unexpectedly, an EOFException
is thrown. Methods of the DataInputStream
class that are used to read data such as readBoolean()
, readByte()
and readChar()
can throw this exception.
Many other input operations return a special value on end of file or stream rather than throwing an exception.
 
EOFException Example
Here's an example of a EOFException
thrown when trying to read all characters from an input file:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class EOFExceptionExample {
public static void main(String[] args) {
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new FileInputStream("myfile.txt"));
while (true) {
inputStream.readChar();
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
In the above example, the contents of a file with the name myfile.txt
are read in an infinite loop using the DataInputStream.readChar()
method. When the readChar()
method is called at the end of the file, an EOFException
is thrown:
java.io.EOFException
at java.base/java.io.DataInputStream.readChar(DataInputStream.java:369)
at EOFExceptionExample.main(EOFExceptionExample.java:13)
 
How to Fix EOFException
Since EOFException
is a checked exception, a try-catch block should be used to handle it. The try
block should contain the lines of code that can throw the exception and the catch
block should catch and handle the exception appropriately.
The above example can be updated to handle the EOFException
in a try-catch block:
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class EOFExceptionExample {
public static void main(String[] args) {
DataInputStream inputStream = null;
try {
inputStream = new DataInputStream(new FileInputStream("myfile.txt"));
while (true) {
try {
inputStream.readChar();
} catch (EOFException eofe) {
System.out.println("End of file reached");
break;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
Here, the inputStream.readChar()
method call is placed in a try
block and the EOFException
is caught inside the catch
block. When an EOFException
occurs at the end of the file, it is handled and a break
statement is used to break out of the loop.
 
Track, Analyze and Manage Errors With Rollbar
Managing errors and exceptions in your code is challenging. It can make deploying production code an unnerving experience. Being able to track, analyze, and manage errors in real-time can help you to proceed with more confidence. Rollbar automates error monitoring and triaging, making fixing Java errors easier than ever. Sign Up Today!