【问题标题】:Having Trouble with ObjectInputStream/OutputStreamObjectInputStream/OutputStream 遇到问题
【发布时间】:2014-01-25 17:34:42
【问题描述】:

我的程序无法将我的地图保存到文件中。这是我编写和读取我的地图和数组列表的两种方法。

这是我的阅读方法:

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。

【问题讨论】:

    标签: serialization map arraylist objectinputstream


    【解决方案1】:
    1. readObject() 不会返回 null,除非你写了一个 null。如果您将其用作流结束的测试,则它是无效的。正确的技巧是捕捉EOFException。

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

    3. available() 也不是流结束的有效测试。那不是它的用途。请参阅 Javadoc。同样,readObject() 的正确技术是捕捉EOFException。

    【讨论】:

    • 我添加了我的 try/catch 块,但每次运行时,即使在将数据写入文件时,即使地图中有数据,我也会不断收到异常。
    • 我给了你大约六件事来修复,而不仅仅是 try/catch 块。您需要全部解决。您编辑中的新代码没有显示任何迹象。
    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 2021-08-09
    • 2017-05-02
    相关资源
    最近更新 更多