第一次用这个字节流 如下
import java.io.*;class Student implements Serializable{
private String name;
public Student(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public String toString(){
return "姓名:"+this.name;
}
}
public class MyDemo{
public static void main(String[] args) throws IOException, ClassNotFoundException{
FileOutputStream fos=new FileOutputStream("m.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(new Student("ahah"));
oos.writeObject(new Student("papa"));
System.out.println("ok!");
oos.close();
fos.close();
//
FileInputStream fis=new FileInputStream("m.dat");
ObjectInputStream ois=new ObjectInputStream(fis);
while(ois.readObject()!=null){//这一行错了,是为什么?
Student stu1=(Student)ois.readObject();
System.out.println(stu1);
}
ois.close();
fis.close();
}
}为什么结果只显示了第二个对象?ok!
姓名:papa
Exception in thread "main" 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 MyDemo.main(MyDemo.java:27)
import java.io.*;class Student implements Serializable{
private String name;
public Student(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public String toString(){
return "姓名:"+this.name;
}
}
public class MyDemo{
public static void main(String[] args) throws IOException, ClassNotFoundException{
FileOutputStream fos=new FileOutputStream("m.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(new Student("ahah"));
oos.writeObject(new Student("papa"));
System.out.println("ok!");
oos.close();
fos.close();
//
FileInputStream fis=new FileInputStream("m.dat");
ObjectInputStream ois=new ObjectInputStream(fis);
while(ois.readObject()!=null){//这一行错了,是为什么?
Student stu1=(Student)ois.readObject();
System.out.println(stu1);
}
ois.close();
fis.close();
}
}为什么结果只显示了第二个对象?ok!
姓名:papa
Exception in thread "main" 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 MyDemo.main(MyDemo.java:27)
解决方案 »
- 关于struts1中如何取得表单form中数据的问题!!!
- 解析excel问题,急急急..........
- 有多年c/c++编程经验,请教如何安排java的学习课程?
- 什么情况下方法可以自动调用?很不解,求解释,多谢!
- 请问JAVA用什么编译器好?
- 在java applet中的URL类要有哪个引用啊
- 循环语句报错
- String str = "0xC4"怎么转换成 byte型 ,值为0xC4,大虾别见笑
- 很高兴大家对我前面帖子的帮助,现在还有一点儿疑惑,望指教!
- 请问:java调用oracel问题
- 关于CLASS类的一个疑问
- 请教org.springframework.web.context.ContextLoaderListener配置struts2和spring以及路径关系
Student stu1=(Student)ois.readObject();//这次读取了papa
System.out.println(stu1);
}然后再去执行循环 while(ois.readObject //第三次读取报错。
你仔细看看这句话,里面只有两个对象,你读取了两次。第三次去读取的时候当然到达文件末端了)end of file )EOFExcepiton了。
public static void main(String[] args) throws IOException, ClassNotFoundException{
FileOutputStream fos=new FileOutputStream("m.dat");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(new Student("ahah"));
oos.writeObject(new Student("papa"));
System.out.println("ok!");
oos.close();
fos.close();
//
FileInputStream fis=new FileInputStream("m.dat");
ObjectInputStream ois=new ObjectInputStream(fis);
Student s; //定义Student类变量.
while((fis.available()>0)&&(s= //判断是否到文件尾。
(Student)ois.readObject())!=null){
//Student stu1=(Student)ois.readObject(); //再运行就读了下一个了。
System.out.println(s);
}
ois.close();
fis.close();
}
}