从.dat文件

时间:2018-05-12 23:42:11

标签: java

我在从.dat文件中加载对象时出现问题。我相信他们写得很好。我觉得这个解决方案非常简单,但是我已经把头撞到墙上好几个小时了。这两种方法都没有抛出任何错误。如果我运行save2方法,我的输出是这样的:

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

这是运行load2方法的输出:

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

但是这些物品并没有被放入记忆中。有1名教师和2名学生获得了拯救。负载方法甚至没有接纳教师。

这是我的保存方法:

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

这是我的加载方法:

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

}

另外他添加(Person ... person)方法也可以正常工作。我有一个工作方法,从文本文件加载数据。我有一个类似的方法来加载课程,除了没有inStream.readObject()。toString ... if-statement,它工作正常。我认为这个问题与inStream.readObject()。toString()。startsWith(教师或学生)有关。

这是课程的加载方法,工作正常:

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

0 个答案:

没有答案