我有一個類,其讀取學生[]數組對象java.io.EOFException的上FileOutputStream中
這裏的寫入的輸出是代碼
import java.io.*;
// I use this class to read the final ouput "students_updated.dat"
public class StudentArrayObjectFileReader {
public static void main(String[] args) {
try {
ObjectInputStream fileReader = new ObjectInputStream(new FileInputStream("students_updated.dat"));
Student[] studs = (Student[]) fileReader.readObject();
fileReader.close();
// List the records in console
for (int i = 0; i < studs.length; i++) {
System.out.printf("%7d %-35s %-5s %1d %-6s%n",studs[i].getID(), studs[i].getName(), studs[i].getCourse(), studs[i].getYr(), studs[i].getGender());
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
問題是,它讀出時有錯誤在Student[] studs = (Student[]) fileReader.readObject();
這裏說
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at StudentArrayObjectFileReader.main(StudentArrayObjectFileReader.java:9)
上的任何想法是什麼可能是問題?在此先感謝..
這是怎麼students_updated.dat
寫
public void saveStudentArray() { // studs print to student_updated.dat
try{
output.writeObject(studs); // write the final studs
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
在哪裏這個方法是
的方式寫對象之前它是事實該對象數組未被序列化。謝謝! – 2012-07-06 17:10:54