How to read DataInputStream until the end without need to catch an EOFException in java?


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.

In 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.

Example

Following program demonstrates how to handle the EOFException in Java.

import java.io.DataInputStream;
import java.io.DataOutputStream;
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
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a String: ");
      String data = sc.nextLine();
      byte[] buf = data.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();
      //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);
      }
   }
}

Output

Enter a String:
hello how are you
helException in thread "main" lo how are youjava.io.EOFException
   at java.io.DataInputStream.readChar(Unknown Source)
   at MyPackage.AIOBSample.main(AIOBSample.java:27)

Reading DataInputStream without catching exception

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.

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
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a String: ");
      String data = sc.nextLine();
      byte[] buf = data.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();
      //Reading from the above created file using readChar() method
      File file = new File("D:\\data.txt");
      FileInputStream fis = new FileInputStream(file);
      byte b[] = new byte[(int) file.length()];
      fis.read(b);
      System.out.println("contents of the file: "+new String(b));
   }
}

Output

Enter a String:
Hello how are you
contents of the file: H e l l o h o w a r e y o u
raja
Published on 05-Aug-2019 18:22:22
Advertisements