ObjectInputStream/OutputStream 有问题 [英] Having Trouble with ObjectInputStream/OutputStream

查看:35
本文介绍了ObjectInputStream/OutputStream 有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am having trouble with my programs ability to save my Maps to a file. Here are my two methods for writing and reading my maps and 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 个不同的对象,如果有办法将它保存到一个文件中,请告诉我,但我遇到了很多我无法弄清楚的错误如何解决所以这就是我最终要做的.感谢您提供的任何帮助.

Sorry if it is a little confusing I have 3 files to save 3 different objects, if there is a way to save it into one file let me know but I just was getting a lot of errors that I couldn't figure out how to solve so this is what I ended up doing. Thanks for any help given.

致 EJP:我试过了

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

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

And even when there was data in it when it was written to the file, I got an EOFException everytime.

推荐答案

  1. readObject() 不会返回 null,除非你写了一个 null.如果您将其用作流结束的测试,则它无效.正确的技术是捕获EOFException.

  1. readObject() doesn't return null unless you wrote a null. If you're using that as a test for end of stream, it is invalid. The correct technique is to catch EOFException.

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

You are calling it and throwing away the result if it isn't null, and then calling it again. The second call will throw EOFException if there isn't another object in the file. It won't give you the same result as the first call. It's a stream.

available() 也不是流结束的有效测试.这不是它的目的.请参阅 Javadoc.同样,使用 readObject() 的正确技术是捕获 EOFException.

available() is also not a valid test for end of stream. That's not what it's for. See the Javadoc. Again, the correct technique with readObject() is to catch EOFException.

这篇关于ObjectInputStream/OutputStream 有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆