gpt4 book ai didi

java - 从 .dat 文件加载对象

转载 作者:行者123 更新时间:2023-12-02 11:15:30 25 4
gpt4 key购买 nike

我在从 .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

}

他的 add(Person... person) 方法也可以正常工作。我有一种从文本文件加载数据的工作方法。我有一个类似的方法来加载类(class),除了没有 inStream.readObject().toString... if 语句,并且它工作正常。我认为这个问题与 inStream.readObject().toString().startsWith( 教师或学生) 有关。

这是类(class)的加载方法,效果很好:

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

最佳答案

您应该读取一个对象,而不是读取一个对象并将其转换为字符串并将其丢弃并查看字符串以什么开头,然后读取另一个对象并使用 instanceof 来查看它实际上是什么。

关于java - 从 .dat 文件加载对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50311553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com