javaeofexception

java.io.EOFException being thrown


This code is throwing an java.io.EOFException, and I am not sure why this is happening.

import java.io.*;

class ReadInts {
    public static void main(String[] args) {
        String fileName = "intData.dat";

        int sum = 0;

        try {
            DataInputStream instr = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));

            while (true) {
                sum += instr.readInt();
                System.out.println("The sum is: " + sum);
                sum += instr.readInt();
                System.out.println("The sum is: " + sum);
                sum += instr.readInt();
                System.out.println("The sum is: " + sum);
                sum += instr.readInt();

                System.out.println("The sum is: " + sum);
                instr.close();
            }
        } catch (EOFException e) {
            System.out.println("EOF reached for: " + fileName);
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("File " + fileName + " not found.");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Problem reading " + fileName);
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

The contents of the input file is:

0
1
255
-1

There is no return character of line feed after -1.

The output I receive is:

The sum is: 805974282
The sum is: 1648322068
EOF reaced for: intData.dat
null
java.io.EOFException
        at java.io.DataInputStream.readInt(DataInputStream.java:392)
        at ReadInts.main(ReadInts.java:18)

The output is completely unexpected, and I assume the exception is being thrown because, for whatever reason, the value of sum is great than the maximum value of an int.

I tried changing "int sum = 0" to "long sum = 0" and received the same results.

I commented out the following:

        sumOfInts += instr.readInt();
        System.out.println("The sum is: " + sumOfInts);
        sumOfInts += instr.readInt();
        // System.out.println("The sum is: " + sumOfInts);
        // sumOfInts += instr.readInt();
        // System.out.println("The sum is: " + sumOfInts);
        // sumOfInts += instr.readInt();

After doing this, I received the following exception:

The sum is: 805974282
The sum is: 1648322068
Problem reading intData.dat
Stream closed
java.io.IOException: Stream closed
        at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:269)
        at java.io.DataInputStream.readInt(DataInputStream.java:387)
        at ReadInts.main(ReadInts.java:14)

If it helps, I am using Ubuntu 18.04 LTS.

java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

Thanks for any help.

Tony


Solution

  • The problem is that readInt is not reading a string and convert the string to a number; it reads four input bytes and returns an int value which it calculates using binary arithmetic.

    0, \n(13), 1, \n(13) is 1st readInt
    2, 5, 5, \n(13) is 2nd readInt
    2 is third readInt after which you will get EOF exception
    

    One more suggestion would be to close objects like stream in finally block

    public static void main(String[] args) throws Exception {
    
            String fileName = "C:\\rsc\\intdat.dat";
    
            int sum = 0;
    
            DataInputStream instr=null;
            try {
                instr = new DataInputStream(new BufferedInputStream(new FileInputStream(fileName)));
    
                while (instr.available()!=0) {
                    sum += Integer.parseInt(instr.readLine());
                    System.out.println("The sum is: " + sum);
                }
            } catch (EOFException e) {
                System.out.println("EOF reached for: " + fileName);
                System.out.println(e.getMessage());
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                System.out.println("File " + fileName + " not found.");
                e.printStackTrace();
            } catch (IOException e) {
                System.out.println("Problem reading " + fileName);
                System.out.println(e.getMessage());
                e.printStackTrace();
            }
            finally{
                if(instr!=null)
                instr.close();
            }
    
        } 
    

    PS: InputStream is a binary construct. If you want to read text data use BufferedReader instead