Having Trouble with ObjectInputStream/OutputStream

两盒软妹~` 提交于 2019-12-11 18:15:35

问题


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.

Here is my read method:

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();
        }
    }
}

And Here is my Writing method:

    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);
}

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.

To EJP: I tried this

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

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


回答1:


  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.

  2. 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.

  3. 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.



来源:https://stackoverflow.com/questions/21353846/having-trouble-with-objectinputstream-outputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!