0

I'm having an issue loading objects back from my .dat file. I believe they're writing properly. I feel like the solution is pretty easy, but I've been banging my head against the wall for hours over this. Neither methods are throwing any errors. If I run the save2 method, my output is this:

Obj written: [Faculty] Professor John Example [x] 123-010-1010 [x] ID: 1 [x] Salary: $63,605.00
Obj written: [Student] Ron Doe [x] 123-222-2332 [x] Major: Culinary [x] ID: 2 [x] GPA: 3.7 [x] courseBag
Obj written: [Student] Bon Jovi [x] 123-372-4383 [x] Major: Computer Science [x] ID: 3 [x] GPA: 2.85 [x] courseBag

This is the output of running the load2 method:

FOUND A STUDENT---------
[PeopleBag]: Loaded people_bag2.dat into memory successfully.

But the objects aren't getting placed into memory. There's 1 faculty and 2 students that were saved. The load method isn't even picking up the faculty member.

Here's my save method:

public void save2() {
    String fileName;
    FileOutputStream outFile;
    ObjectOutputStream outStream;
    Person tempPerson;

    fileName = "people_bag2.dat";

    try {
        outFile = new FileOutputStream(fileName);
        outStream = new ObjectOutputStream(outFile);

        for (int i = 0; i < personArray.length; i++) {
            if(personArray[i] != null) {
                tempPerson = personArray[i];
                outStream.writeObject(tempPerson); // this one line writes an object
                if(Utilities.DEBUG)
                    System.out.println("Obj written: "+tempPerson);
            }
        }

        outStream.close();
        if(Utilities.DEBUG)
            System.out.println("[PeopleBag]: Saved bag to "+fileName+" successfully.");
    } catch (IOException e) {
        System.out.println(e);
    }
}

Here's my load method:

public void load2() {
    String fileName;
    FileInputStream inFile;
    ObjectInputStream inStream = null;
    Student tempStudent;
    Faculty tempFaculty;

    fileName = "people_bag2.dat";

    try {
        inFile = new FileInputStream(fileName);
        inStream = new ObjectInputStream(inFile);

        while (true) {
            if(inStream.readObject().toString().startsWith("[Student")) {
                System.out.println("FOUND A STUDENT---------");

                tempStudent = (Student) inStream.readObject();
                Student person = new Student(tempStudent.getFirstName(), tempStudent.getLastName(), tempStudent.getPhoneNumber(), tempStudent.getMajor(), tempStudent.getCourseBag());
                add(tempStudent); //add(person) doesn't work either
                //if(Utilities.DEBUG)
                    System.out.println("tempStudent: "+tempStudent.toString() + "\n");

            }

            if(inStream.readObject().toString().startsWith("[Faculty")) {
                System.out.println("FOUND A FACULTY---------");

                tempFaculty = (Faculty) inStream.readObject();
                //String firstName, String lastName, String phoneNumber, String title, double salary
                Faculty f = new Faculty(tempFaculty.getFirstName(), tempFaculty.getLastName(), tempFaculty.getPhoneNumber(), tempFaculty.getTitle(), tempFaculty.getSalary());
                add(f); //add the person to the bag
            }

        }


    } catch (EOFException e) { // catch EOF
        try {
            if(Utilities.DEBUG)
                System.out.println("[PeopleBag]: Loaded "+fileName+" into memory successfully.");
            inStream.close();
        } exceptions blah blah

}

Also he add(Person... person) method works fine. I have a working method which loads the data from a text file. I have a similar method to load courses, except without the inStream.readObject().toString... if-statement, and it works fine. I think the issue has to do with the inStream.readObject().toString().startsWith( faculty or student).

This is a load method for courses, which works fine:

public void load() {
    String fileName = "course_bag.dat";
    FileInputStream inFile;
    ObjectInputStream inStream = null;
    Course tempCourse;

    try {
        inFile = new FileInputStream(fileName);
        inStream = new ObjectInputStream(inFile);

        while (true) {
            tempCourse = (Course)inStream.readObject();
            //String courseTitle, String crn, Textbook textbook, double credits
            Course txtbk = new Course(tempCourse.getCourseTitle(), tempCourse.getCrn(), tempCourse.getTextbook(), tempCourse.getCredits());
            add(txtbk);
        }

    } catch (FileNotFoundException e) {
        System.out.println("File named "+ fileName +" not found.\n");
    } catch (EOFException e) { // catch EOF
        try {
            if(Utilities.DEBUG)
                System.out.println("[CourseBag]: Loaded "+fileName+" into memory successfully.");
            inStream.close();
        } catch (IOException ex) { }
    } catch (IOException e) {
        System.out.println(e);
    } catch (ClassNotFoundException e) {
        System.out.println(e);
    }
}
3
  • I love using JSON, but this is a project for school and it has to be done in this way. Commented May 12, 2018 at 23:51
  • @Gala I added more information, check my edit. I have another class using a similar method and it's working. Commented May 12, 2018 at 23:56
  • @Gala Neitther changing the technology nor the number of files nor the technique into a single file has anything to do with this problem. Please stop guessing. Commented May 13, 2018 at 0:21

1 Answer 1

1

Instead of reading an object and converting it to a string and throwing it away and seeing what the string starts with and then reading another object, you should read one object and use instanceof to see what it actually was.

Sign up to request clarification or add additional context in comments.

1 Comment

Didn't even think of instanceof. Thank you!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.