gpt4 book ai didi

java - 从Java中的文件读取错误

转载 作者:行者123 更新时间:2023-12-02 11:05:46 25 4
gpt4 key购买 nike

我写了以下程序,从文本фалйа中读取计算机的数量,其中包含质数

import java.io.*;

public class ReadFromFile {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

int CountComputers=0;
try {
DataInputStream ConfigFile = new DataInputStream( new
FileInputStream("D:\\HadoopFolder\\ReadFromFile\\src\\countcomputers.txt"));
CountComputers=ConfigFile.readInt();
ConfigFile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//CountComputers=ConfigFile.readInt();
System.out.println("Количество компьютеров равно "+CountComputers);
}

}

结果,我收到以下错误
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readInt(Unknown Source)
at ReadFromFile.main(ReadFromFile.java:16)

我是否也肯定有一个问题,是否有必要在try ... expect中结束从文件中的读取(在一个项目中强调红色,而在其他项目中也强调java(这是另一种))。我是Java的初学者,因此要求理解。

最佳答案

java.io.EOFException
-此异常主要由数据输入流用于信号流结束。注意,许多其他输入操作在流的末尾返回一个特殊值,而不是引发异常。

我相信您正在读取的文件中没有int
尝试这个CountComputers=Integer.valueOf(ConfigFile.readLine());

关于java - 从Java中的文件读取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12976760/

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