遇到ObjectInputStream/OutputStream的问题 [英] Having Trouble with ObjectInputStream/OutputStream

查看:61
本文介绍了遇到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.

推荐答案

    除非您写入null,否则
  1. readObject()不会返回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.

您正在调用它,如果结果不为null,则将其丢弃,然后再次调用它.如果文件中没有其他对象,则第二个调用 将引发 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天全站免登陆