この例外が発生しています。私が捕まえていないからでしょうか?
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2577)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at ReadStudentRecord.readRecord(ReadStudentRecord.java:34)
at ReadStudentRecordTest.main(ReadStudentRecordTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
これは、バイナリ ファイルを読み取る私のクラスです。
import java.io.*;
public class ReadStudentRecord {
private ObjectInputStream input;
public void openFile() {
try {
input = new ObjectInputStream(new FileInputStream("student.dat"));
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} // end of open file method
public void readRecord() {
Q4 student;
System.out.println("Student Name\t\t Student average Mark");
try {
while (true) {
student = (Q4) input.readObject();
System.out.println(student.getStudentName() + "\t\t\t" + student.averageMark(student.getMark()));
}
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}// end of readRecord method
public void closeFile() {
try // close file and exit
{
if (input != null)
input.close();
System.exit(0);
} // end try
catch (IOException ioException) {
System.err.println("Error closing file.");
System.exit(1);
} // end catch
} // end method closeFile
} // end of class
上記の例外が発生することを除いて、問題なく動作しますが、何が原因なのかわかりません。私は自分が間違っていることを理解できません。