【问题标题】:Reading unknown number of objects from file从文件中读取未知数量的对象
【发布时间】:2015-07-13 23:47:27
【问题描述】:

任务是

创建一个程序来接受和存储来自任何 Winter 的数据 奥运会。 该程序应接受并存储每个赛事最多 6 个参赛者姓名 以及他们参加的活动的名称。它也应该排序 仅显示奖牌获得者的项目得分或次数 他们赢得的奖牌。

我创建了一个运行顺利的“事件”类,但是在尝试将“事件”对象写入文件并读取它们时遇到了问题。

这是我创建新事件的方法:

public static void addEvent() {
    File events = new File("events.dat");
    Scanner input = new Scanner(System.in);
    String eventName;
    ArrayList<Event> olympics = new ArrayList<Event>();
    boolean fileExists = false;

    if (events.exists()) {
        fileExists = true;
    } else {
        try {
            events.createNewFile();
            System.out.println("File created");
        } catch (IOException e) {
            System.out.println("File could not be created");
            System.err.println("IOException: " + e.getMessage());
        }
    }

    System.out.print("Enter name of event: ");
    eventName = input.nextLine();
    olympics.add(new Event(eventName));

    //write Objects
    try {
        FileOutputStream out = new FileOutputStream(events, fileExists);
        ObjectOutputStream writeEvent = new ObjectOutputStream(out);
        writeEvent.writeObject(olympics);
        writeEvent.close();
    } catch (FileNotFoundException e) {
        System.out.println("File could not be found.");
        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Problem with input/output.");
        System.err.println("IOException: " + e.getMessage());
    }
    menu();
}// end addEvent()

以及我显示文件的方法:

public static void displayFile(File datFile) {
    File events = datFile;
    String output ="";

    //read objects
    try {
        FileInputStream in = new FileInputStream(events);
        ObjectInputStream readEvents = new ObjectInputStream(in);
        ArrayList<Event> recoveredEvents = new ArrayList<Event>();

        while (readEvents.readObject() != null) {
            recoveredEvents.add((Event)readEvents.readObject());
        }
        readEvents.close();
        for (Event e: recoveredEvents) {
            //System.out.println(e + "\n");
            output += e + "\n";
        }
    } catch(FileNotFoundException e) {
        System.out.println("File does not exist or could not be found");
        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("Problem reading file");
        System.err.println("IOException: " + e.getMessage());
    } catch (ClassNotFoundException e) {
        System.out.println("Class could not be used to cast object.");
        System.err.println("ClassNotFoundException: " + e.getMessage());
    }
    System.out.println(output);
    menu();
}// end displayFile()

这是一门初级课程,到目前为止,我很难处理文件。编辑:忘了提到当我尝试调用 displayFile() 时它会抛出一个带有消息“null”的 IOException。

【问题讨论】:

  • 你能发布堆栈跟踪吗?
  • @frenchDolphin 抱歉,我该怎么做?我是初学者,记住 ;)
  • 好的,抱歉。当您收到 IOException 时,您收到的具体错误消息是什么?它应该类似于this;看看帖子的顶部。
  • 该页面上的帖子还解释了堆栈跟踪是什么:请参阅here
  • 我忘记了一些事情,请参阅here。没有它,您将看不到任何堆栈跟踪!

标签: java object serialization


【解决方案1】:
  1. 您收到的是EOFException。当readObject() 到达流的末尾时,它不会返回 null。检查Javadoc。它会抛出 EOFException. 您应该单独捕获它,而不是将其视为需要报告的错误。

  2. 你扔掉了每一个奇怪的元素。您的读取循环应如下所示:

    while (true)
    {
        recoveredEvents.add((Event)readObject.readObject());
    }
    
  3. 您不能追加到由ObjectOutputStream 编写的文件。阅读时到达加入会得到IOException: invalid type code AC。有一些方法可以解决这个问题,但它们并非微不足道。保持文件打开。

  4. new FileOutputStream() 创建文件。之前存在的一切/createNewFile crud 只是浪费时间和空间。

【讨论】:

  • 感谢所有帮助。现在我唯一的问题是关于你的第三点。 “保持文件打开”是什么意思?如果我使用 addEvent() 方法打开文件,该文件是否仍会打开以供我使用 displayFile() 方法?
  • 不调用 close 保持文件打开。
猜你喜欢
  • 2013-11-09
  • 2015-08-01
  • 1970-01-01
  • 2017-03-24
  • 2012-10-05
  • 2012-02-01
  • 1970-01-01
  • 2011-09-26
相关资源
最近更新 更多