我有一个类可以读取 Student[] 数组对象的书面输出
这是代码
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();
}
}
ObjectInputStream
在此方法所在的构造函数中声明