How to handle EOFException in java?

JavaObject Oriented ProgrammingProgramming

While reading the contents of a file in certain scenarios the end of the file will be reached in such scenarios a EOFException is thrown.

Especially, this exception is thrown while reading data using the Input stream objects. In other scenarios a specific value will be thrown when the end of file reached.

Example

Let us consider the DataInputStream class, it provides various methods such as readboolean(), readByte(), readChar() etc.. to read primitive values. While reading data from a file using these methods when the end of file is reached an EOFException is thrown.

import java.io.DataInputStream;
import java.io.FileInputStream;
public class EOFExample {
   public static void main(String[] args) throws Exception {
      //Reading from the above created file using readChar() method
      DataInputStream dis = new DataInputStream(new FileInputStream("D:\\data.txt"));
      while(true) {
         char ch;
         ch = dis.readChar();
         System.out.print(ch);
      }
   }
}

Runtime Exception

Hello how are youException in thread "main" java.io.EOFException
   at java.io.DataInputStream.readChar(Unknown Source)
   at SEPTEMBER.remaining.EOFExample.main(EOFExample.java:11)

Handling EOFException

You cannot read contents of a file without reaching end of the file using the DataInputStream class. If you want, you can use other sub classes of the InputStream interface.

Example

In the following example we have rewritten the above program using FileInputStream class instead of DataInputStream to read data from a file.

 Live Demo

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class AIOBSample {
   public static void main(String[] args) throws Exception {
      //Reading data from user
      byte[] buf = " Hello how are you".getBytes();
      //Writing it to a file using the DataOutputStream
      DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\\data.txt"));
      for (byte b:buf) {
         dos.writeChar(b);
      }
      dos.flush();
      System.out.println("Data written successfully");
   }
}

Output

Data written successfully

Following is another way to handle EOFException in Java −

import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
public class HandlingEOF {
   public static void main(String[] args) throws Exception {
      DataInputStream dis = new DataInputStream(new FileInputStream("D:\\data.txt"));
      while(true) {
         char ch;
         try {
            ch = dis.readChar();
            System.out.print(ch);
         } catch (EOFException e) {
            System.out.println("");
            System.out.println("End of file reached");
            break;
         } catch (IOException e) {
         }
      }
   }
}

Output

Hello how are you
End of file reached
raja
Published on 14-Oct-2019 11:49:05
Advertisements