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

标签 java eclipse file jakarta-ee compiler-errors

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

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/

相关文章:

java - 如何使用 spring 数据在 mongo 聚合中构建 $group 的组合 _id 属性?

android - 扩展 Android 应用程序的最佳做法是什么?

linux - 我需要文件 I/O 的解释'我收到了我不明白的警告

java - 文件输入帮助/建议

java - 闹钟 : repeat setAlarmClock()

java - 如何设置 "volume: - postgres"Windows 10

java - Icefaces 2 插件问题

python - 有没有一种方法可以有效地生成包含数百万个文件的目录中的每个文件?

java - 如何引用 primaryStage

android - 在 Eclipse : no repository found 中安装 ADT 时出错