java如何反序列化一个组对象,就是如何判断读取到了最后一个对象?
我是这样写的,出是出了,但有一行抛了个异常publicclassFileOutIntObject{publicstaticvoidmain(String[]args)th...
我是这样写的,出是出了,但有一行抛了个异常
public class FileOutIntObject {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
List<Emp> list=new ArrayList<Emp>();
list.add(new Emp("zhangsan",21,"male",2000));
list.add(new Emp("lisi",22,"female",3000));
FileOutputStream fos=
new FileOutputStream("c"+File.separator+"Emp.obj");
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(Emp e:list){
oos.writeObject(e);
}
oos.close();
fos.close();
FileInputStream fis=
new FileInputStream("c"+File.separator+"Emp.obj");
ObjectInputStream ois=new ObjectInputStream(fis);
Object obj=null;
while((obj=ois.readObject())!=null){/这一行抛了个异常!!!
Emp e=(Emp)obj;
System.out.println(e);
}
ois.close();
fis.close();
}
} 展开
public class FileOutIntObject {
public static void main(String[] args)
throws IOException, ClassNotFoundException {
List<Emp> list=new ArrayList<Emp>();
list.add(new Emp("zhangsan",21,"male",2000));
list.add(new Emp("lisi",22,"female",3000));
FileOutputStream fos=
new FileOutputStream("c"+File.separator+"Emp.obj");
ObjectOutputStream oos=new ObjectOutputStream(fos);
for(Emp e:list){
oos.writeObject(e);
}
oos.close();
fos.close();
FileInputStream fis=
new FileInputStream("c"+File.separator+"Emp.obj");
ObjectInputStream ois=new ObjectInputStream(fis);
Object obj=null;
while((obj=ois.readObject())!=null){/这一行抛了个异常!!!
Emp e=(Emp)obj;
System.out.println(e);
}
ois.close();
fis.close();
}
} 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询