EOFException

yanghe0621 2010-06-08 08:32:04
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)
...全文
102 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Java技术栈 2010-06-09
  • 打赏
  • 举报
回复
java.io.EOFException 读到文件尾了程序还在读。。
  • 打赏
  • 举报
回复
我运行了 没错啊

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧