EOFException
import java.io.*;
public class Student implements Serializable{
public int no;
public String name;
public int grade;
public void stdRead(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//no = Integer.parseInt(in.readLine());
name = in.readLine();
grade =Integer.parseInt(in.readLine());
}catch(IOException e){
System.err.print("Student():Exception!!"+e);
e.printStackTrace();
}
}
public void myOWrite(){
try{
ObjectOutputStream oos= new ObjectOutputStream(new FileOutputStream("data.txt"));
oos.writeObject(this);
oos.flush();
oos.close();
}catch(IOException e){
System.err.print("myOWrite():Exception!!"+e);
e.printStackTrace();
}
}
public static double myOAve(Student [] stu){
double sum = 0;
int i = 0;
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("data.txt"));
while(ois.readObject() != null){
stu[i] = (Student)ois.readObject();
sum +=stu[i].grade;
i++;
}
ois.close();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException ce){
ce.printStackTrace();
}
return sum/(i+1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Student [] stu = new Student[2];
for(int i = 0;i<2;i++){
stu[i] = new Student();
stu[i].stdRead();
stu[i].myOWrite();
}
Student [] s = new Student[2];
System.out.print(Student.myOAve(s));
}
}
为什么会出现以下错误
的
60
我
60
java.io.EOFException
0.0 at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at Student.myOAve(Student.java:63)
at Student.main(Student.java:107)