从文件中读取未知数量的对象

时间:2015-07-13 23:47:27

标签: java object serialization

作业是

  

创建一个程序,接受并存储任何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。

1 个答案:

答案 0 :(得分:2)

  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之前只是浪费时间和空间。
相关问题