引发java.io.EOFException

时间:2018-08-31 14:24:52

标签: java eofexception

此代码引发了java.io.EOFException,我不确定为什么会发生这种情况。

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

输入文件的内容为:

0
1
255
-1

-1后没有换行符。

我收到的输出是:

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)

输出完全是意外的,并且我认为抛出异常是因为无论出于什么原因,sum的值都大于int的最大值。

我尝试将“ int sum = 0”更改为“ long sum = 0”并收到相同的结果。

我评论了以下内容:

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

这样做之后,我收到以下异常:

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)

如果有帮助,我正在使用Ubuntu 18.04 LTS。

java版本“ 1.8.0_181” Java(TM)SE运行时环境(内部版本1.8.0_181-b13) Java HotSpot(TM)64位服务器VM(内部版本25.181-b13,混合模式)

感谢您的帮助。

托尼

2 个答案:

答案 0 :(得分:3)

问题在于readInt不会读取字符串并将该字符串转换为数字。它 读取四个输入字节并返回一个int值,它使用二进制算术来计算。

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

另一个建议是在finally块中关闭流之类的对象

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是二进制构造。如果您想读取文本数据,请使用BufferedReader

答案 1 :(得分:1)

DataInputStream以字节为单位(4个字节)中的字节形式读取输入。 所以
0
1
255
-1
0,\ n,1,\ n是一个readInt
2、5、5,\ n是其他readInt
-1是第三个readInt
因此,在三个readInt之后,由于文件已完成,您将获得EOF异常。
另外,您应该纠正威尔循环。