0 votes
9 views
in Java by (9.5k points)

Below is the code I’ve from the Java tutorials's Data Streams Page:

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);

    }

}

When I run it gives me the output like below:

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.

       But during the runtime, I get the error “EOFException”. 

Can anyone tell me how to handle this?

1 Answer

0 votes
by (19.7k points)

You have to terminate the loop, while you read from the file. That’s the reason you get an EOFException on the next iteration for the below line even though it reads all the values. 

price = in.readDouble();

See the below code implementation to put termination condition in the while loop:

 while(in.available() > 0)  <--- if there are still bytes to read

Interested in Java? Check out this Java Certification by Intellipaat.    

Related questions

0 votes
1 answer
0 votes
1 answer
asked Oct 30, 2020 in Python by Sudhir_1997 (55.5k points)
0 votes
1 answer
asked Oct 30, 2020 in Python by Sudhir_1997 (55.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !

Categories

...