Java 从.dat文件加载对象
从.dat文件中加载对象时遇到问题。我相信他们写得很好。我觉得解决方法很简单,但我已经为此头痛了好几个小时了。这两种方法都没有抛出任何错误。如果运行save2方法,我的输出如下:Java 从.dat文件加载对象,java,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: Culin
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);
}
}
此外,他还添加了(Person…Person)方法。我有一个从文本文件加载数据的工作方法。我有一个类似的加载课程的方法,除了没有inStream.readObject().toString。。。if语句,它工作正常。我认为这个问题与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);
}
}
与其读取一个对象并将其转换为字符串,然后将其丢弃,然后查看字符串的开头,然后读取另一个对象,不如读取一个对象并使用
instanceof
查看它的实际内容。我喜欢使用JSON,但这是学校的项目,必须以这种方式完成。@Gala我添加了更多信息,请检查我的编辑。我有另一个类使用类似的方法,它正在工作。@Gala Neither将技术、文件数或技术更改为单个文件与此问题无关。请别猜了,我都没想到。非常感谢。
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);
}
}