Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOFException - how to handle?

Tags:

I'm a beginner java programmer following the java tutorials.

I am using a simple Java Program from the Java tutorials's Data Streams Page, and at runtime, it keeps on showing EOFException. I was wondering if this was normal, as the reader has to come to the end of the file eventually.

import java.io.*;  public class DataStreams {     static final String dataFile = "F://Java//DataStreams//invoicedata.txt";      static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };     static final int[] units = { 12, 8, 13, 29, 50 };     static final String[] descs = {         "Java T-shirt",         "Java Mug",         "Duke Juggling Dolls",         "Java Pin",         "Java Key Chain"     };     public static void main(String args[]) {         try {             DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dataFile)));              for (int i = 0; i < prices.length; i ++) {                 out.writeDouble(prices[i]);                 out.writeInt(units[i]);                 out.writeUTF(descs[i]);             }              out.close();           } catch(IOException e){             e.printStackTrace(); // used to be System.err.println();         }          double price;         int unit;         String desc;         double total = 0.0;          try {             DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile)));              while (true) {                 price = in.readDouble();                 unit = in.readInt();                 desc = in.readUTF();                 System.out.format("You ordered %d" + " units of %s at $%.2f%n",                 unit, desc, price);                 total += unit * price;             }         } catch(IOException e) {             e.printStackTrace();          }          System.out.format("Your total is %f.%n" , total);     } } 

It compiles fine, but the output is:

You ordered 12 units of Java T-shirt at $19.99 You ordered 8 units of Java Mug at $9.99 You ordered 13 units of Duke Juggling Dolls at $15.99 You ordered 29 units of Java Pin at $3.99 You ordered 50 units of Java Key Chain at $4.99 java.io.EOFException         at java.io.DataInputStream.readFully(Unknown Source)         at java.io.DataInputStream.readLong(Unknown Source)         at java.io.DataInputStream.readDouble(Unknown Source)         at DataStreams.main(DataStreams.java:39) Your total is 892.880000. 

From the Java tutorials's Data Streams Page, it says:

Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.

So, does this mean that catching EOFException is normal, so just catching it and not handling it is fine, meaning that the end of file is reached?

If it means I should handle it, please advise me on how to do it.

EDIT

From the suggestions, I've fixed it by using in.available() > 0 for the while loop condition.

Or, I could do nothing to handle the exception, because it's fine.

like image 561
Jonathan Lam Avatar asked Aug 26 '13 19:08

Jonathan Lam


People also ask

What causes EOFException?

Signals that an end of file or end of stream has been reached unexpectedly during input. This exception is mainly used by data input streams to signal end of stream. Note that many other input operations return a special value on end of stream rather than throwing an exception.

What is file not found exception in Java?

Class FileNotFoundExceptionSignals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.

What is DataInputStream class in Java?

public class DataInputStream extends FilterInputStream implements DataInput. A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.


2 Answers

The best way to handle this would be to terminate your infinite loop with a proper condition.

But since you asked for the exception handling:

Try to use two catches. Your EOFException is expected, so there seems to be no problem when it occures. Any other exception should be handled.

... } catch (EOFException e) {    // ... this is fine } catch(IOException e) {     // handle exception which is not expected     e.printStackTrace();  } 
like image 36
Martin Seeler Avatar answered Sep 22 '22 11:09

Martin Seeler


While reading from the file, your are not terminating your loop. So its read all the values and correctly throws EOFException on the next iteration of the read at line below:

 price = in.readDouble(); 

If you read the documentation, it says:

Throws:

EOFException - if this input stream reaches the end before reading eight bytes.

IOException - the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.

Put a proper termination condition in your while loop to resolve the issue e.g. below:

     while(in.available() > 0)  <--- if there are still bytes to read 
like image 90
Yogendra Singh Avatar answered Sep 19 '22 11:09

Yogendra Singh