EOF exception in Java
The EOFException (End of File) is a checked exception in Java that occurs when an end of file or end of stream is reached unexpectedly during input. Data input streams mainly use it to signal the end of a stream. Many other input operations return a special value at the end of the stream rather than throwing an exception.
try { // Read data from the file. } catch (EOFException e) { // Handle the exception. }
Another way to handle an EOFException is to use the throws clause. The throws clause allows you to declare that a method can throw an EOFException.
public void readDataFromFile() throws EOFException { // Read data from the file. }
Reasons for an End of File Exception
- Trying to read past the end of a file or stream.
- Trying to read data from a stream that has been closed.
- A network error.
- A bug in the program that is trying to read data.
If the end of the file or stream is unexpectedly reached while reading the contents of a file or stream using DataInputStream objects, an EOFException is issued. This exception may be thrown by data reading methods of the DataInputStream class, including readBoolean(), readByte(), and readChar().
To avoid an End of File Exception, you should always check if you have reached the end of a file or stream before trying to read from it. You can do this by using the available() method of the InputStream class. If available() returns 0, then there is no more data to read from the stream.
EOFException Example
Let us see an example program that throws an EOFException when trying to read all characters from an input file.
Filename: EOFExample1.java
import java.io.DataInputStream; import java.io.FileInputStream; import java.io.IOException; public class EOFExample1 { public static void main(String[] args) { DataInputStream iStream = null; try { iStream = new DataInputStream(new FileInputStream("file.txt")); while (true) { iStream.readChar(); } } catch (IOException io) { io.printStackTrace(); } finally { try { iStream.close(); } catch (IOException io) { io.printStackTrace(); } } } }
Output:
java.io.EOFException at java.io.DataInputStream.readChar(DataInputStream.java:365) at EOFExample1.main(EOFExample1.java:14)
Explanation:
The DataInputStream is used in the program mentioned above to read an infinite loop of text from a file with the name file.txt.method called readChar. An EOFException is thrown when the readChar() function is called at the end of the file.
Handling of EOFException in Java
EOFException should be handled with a try-catch block because it is a checked exception. The lines of code that can throw an exception should be in the try block, and the catch block should catch and handle the exception.
Let us see the java program to handle the EOFException.
File name: EOFExample2
import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; public class EOFExample2 { public static void main(String[] args) { DataInputStream inputStream = null; try { inputStream = new DataInputStream(new FileInputStream("file.txt")); while (true) { try { inputStream.readChar(); } catch (EOFException eof) { System.out.println("End of the file reached"); break; } catch (IOException io) { io.printStackTrace(); } } } catch (IOException io) { io.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException io) { io.printStackTrace(); } } } }
Output:
End of the file reached
Explanation:
The inputStream.readChar() function call is contained within a try block in this program, and the EOFException is handled inside a catch block. A break statement is used to exit the loop when an EOFException at the end of the file has been handled.