遇到ObjectInputStream / OutputStream问题

时间:2014-01-25 17:34:42

标签: serialization map arraylist objectinputstream

我的程序无法将地图保存到文件中。以下是我编写和阅读地图和arraylist的两种方法。

这是我的阅读方法:

private void getData() throws IOException, ClassNotFoundException {
    File f_Instructors = new File(PSLTrackerInfo.file + "instructors.brent");
    File f_Students = new File(PSLTrackerInfo.file + "students.brent");
    File f_Times = new File(PSLTrackerInfo.file + "times.brent");
    if (f_Instructors.exists()) {
        try (ObjectInputStream in = new ObjectInputStream(new 
                BufferedInputStream(new FileInputStream(f_Instructors)))) {
            //Add theList back in
            if (in.readObject() != null) {
                TreeMap<Instructor, Set<Student>> read = null;
                while(in.available() > 0) {
                    read = (TreeMap<Instructor, Set<Student>>) 
                            in.readObject();
                }
                if (read != null) {
                    for (Instructor key : read.keySet()) {
                        System.out.println(key);
                        Set<Student> values = read.get(key);
                        PSLTrackerInfo.addInstructor(key, values);
                    }
                    System.out.println("Instructors Found! Reading...");
                } else {
                    System.out.println("No instructor data saved.1");
                }
            } else {
                System.out.println("No instructor data saved.2");
            }
            in.close();
        }
    }
    //Add times back in
    if (f_Times.exists()) {
        try (ObjectInputStream in = new ObjectInputStream(new 
                BufferedInputStream(new FileInputStream(f_Times)))) {
            if (in.readObject() != null) {
                TreeMap<Student, ArrayList<Date>> readTimes = null;
                while(in.available() > 0) {
                    readTimes = (TreeMap<Student, ArrayList<Date>>) in.readObject();
                }
                if (readTimes != null) {
                    for (Student key : readTimes.keySet()) {
                        System.out.println(key);
                        ArrayList<Date> values = readTimes.get(key);
                        PSLTrackerInfo.addTimes(key, values);
                    }
                    System.out.println("Dates Found! Reading...");
                } else {
                    System.out.println("No dates saved.");
                }
            } else {
                System.out.println("No dates saved.");
            }
            in.close();
        }
    }
    //Add newStudents back in
    if (f_Students.exists()) {
        try (ObjectInputStream in = new ObjectInputStream(new 
                BufferedInputStream(new FileInputStream(f_Students)))) {
            if (in.readObject() != null) {
                ArrayList<Student> readStudents = null;
                while (in.available() > 0) {
                    readStudents = (ArrayList<Student>) in.readObject();

                }
                if (readStudents != null) {
                    PSLTrackerInfo.setTheList(readStudents);
                }
                System.out.println("New students found! Reading...");
            } else {
                System.out.println("No new students data saved.");
            }
            in.close();
        }
    }
}

这是我的写作方法:

    private void saveData() {
    System.out.println("Saving Data...");
    File f_Instructors = new File(PSLTrackerInfo.file + "instructors.brent");
    File f_Students = new File(PSLTrackerInfo.file + "students.brent");
    File f_Times = new File(PSLTrackerInfo.file + "times.brent");
    ObjectOutputStream out_Instructors = null;
    ObjectOutputStream out_Students = null;
    ObjectOutputStream out_Times = null;
    try {
        out_Instructors = new ObjectOutputStream(new 
                BufferedOutputStream(new FileOutputStream(f_Instructors)));
        out_Students = new ObjectOutputStream(new 
                BufferedOutputStream(new FileOutputStream(f_Students)));
        out_Times = new ObjectOutputStream(new 
                BufferedOutputStream(new FileOutputStream(f_Times)));
        out_Instructors.writeObject(PSLTrackerInfo.getMap());
        out_Times.writeObject(PSLTrackerInfo.getTimes());
        out_Students.writeObject(PSLTrackerInfo.getList());
        out_Instructors.flush();
        out_Students.flush();
        out_Times.flush();
        out_Instructors.close();
        out_Students.close();
        out_Times.close();
    } catch (IOException ex) {
        Logger.getLogger(PrivateLessonsTrackerGUI.class.getName())
                .log(Level.SEVERE, null, ex);
    }
    System.exit(0);
}

很抱歉,如果有点混乱我有3个文件来保存3个不同的对象,如果有办法将它保存到一个文件让我知道,但我只是得到了很多我无法弄清楚的错误怎么解决所以这就是我最终做的事情。感谢您给予的任何帮助。

致EJP:我试过这个

TreeMap<Instructor, Set<Student>> read = null;
try {
    read = (TreeMap<Instructor, Set<Student>>) 
            in.readObject();
} catch (EOFException e) {
    System.out.println("Caught EOFException!");
}

即使在写入文件时有数据,我每次都会收到EOFException。

1 个答案:

答案 0 :(得分:0)

    除非您写入null,否则
  1. readObject()不会返回null。如果您将其用作流结束的测试,则无效。正确的方法是捕获EOFException

  2. 您正在调用它并丢弃结果(如果它不为null),然后再次调用它。如果文件中没有其他对象,则第二次调用抛出EOFException。它不会给你与第一次通话相同的结果。这是一个流。

  3. available()也不是流结束的有效测试。这不是它的用途。见Javadoc。同样,使用readObject()的正确技巧是捕获EOFException