java.io.EOFException 被抛出

java.io.EOFException being thrown

此代码抛出 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后没有return个换行符

我收到的输出是:

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 运行时环境(build 1.8.0_181-b13) Java HotSpot(TM) 64 位服务器 VM(内部版本 25.181-b13,混合模式)

感谢您的帮助。

托尼

DataInputStream 将输入读取为 4 个块中的字节。 所以,
0
1
255
-1
0, \n, 1, \n 是一个 readInt
2, 5, 5, \n 是其他的 readInt
-1 是第三个 readInt
所以,在三个 readInt 之后,你会得到 EOF 异常,因为文件已经完成。
此外,您应该更正 wile 循环。

问题是 readInt 不是读取字符串并将字符串转换为数字;它 读取四个输入字节和 returns 一个使用二进制算术计算的 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