gpt4 book ai didi

java.io.EOFException 被抛出

转载 作者:行者123 更新时间:2023-12-02 01:57:26 25 4
gpt4 key购买 nike

此代码抛出 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,混合模式)

感谢您的帮助。

托尼

最佳答案

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

关于java.io.EOFException 被抛出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52117535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com