나는 그것이 파일을로드에 관해서 그러나 나는 (저장 모두 OK입니다)Java에서 파일 오류로드 중?
public static void loadStudentList() {
boolean endOfFile = false;
try {
// create a FileInputStream object, studentFile
FileInputStream studentFile = new FileInputStream("Students.obf");
// create am ObjectImnputStream object to wrap around studentStream
ObjectInputStream studentStream = new ObjectInputStream(studentFile) ;
// read the first (whole) object with the readObject method
Student tempStudent = (Student) studentStream.readObject();
while (endOfFile != true) {
try {
tempStudent = (Student) studentStream.readObject();
stud1.add(tempStudent);
}
catch(EOFException e) {
endOfFile = true;
}
}
studentStream.close();
//use the fact that the readObject throws an EOFException to check whether the end of eth file has been reached
}
catch(FileNotFoundException e) {
System.out.println("File not found");
}
catch(ClassNotFoundException e) { // thrown by readObject
/* which indicates that the object just read does not correspond to any class
known to the program */
System.out.println("Trying to read an object of an unkonown class");
}
catch(StreamCorruptedException e) { //thrown by constructor
// which indicates that the input stream given to it was not produced by an ObjectOutputStream object
System.out.println("Unreadable File Format");
}
catch(IOException e) {
System.out.println("There was a problem reading the file");
}
}
이 내가 파일을로드하는 데 사용되는 코드 문제가 발생하고, 자바 프로젝트에 대한 오브젝트 파일을 구현해야합니다. 프로그램은 내 파일의 마지막 두 레코드 인 만을로드합니다. 아이디어는 프로그램에서 나중에 사용할 수 있도록 모든 프로그램을 배열 목록에로드하는 것입니다. 또한 나는 어떤 포로도 돌려받지 못한다. 어떤 도움이 필요합니까? 감사합니다 :)
처음으로 검색되는 Student 인스턴스는 저장되지 않습니다. stud1 컬렉션. 파일에 3 개의 레코드 만 포함되어있을 수 있습니까? – Henrik
몇 개의 개체를로드 할 예정입니까? 파일의 내용을 게시하십시오. – TechSpellBound