我现在先序列化一些对象到一个文件中,然后反序列化把他们都读出来,为什么会出现异常??
程序如下
/**
 * @(#)TestObjectStream.java
 *
 *
 * @author 
 * @version 1.00 2009/12/11
 */import java.io.Serializable;
import java.io.FileInputStream;
import java.io.*;
public class TestObjectStream {    public static void main (String[] args) {
    
     FileInputStream fis=null;
     FileOutputStream fos=null;
     ObjectInputStream ois=null;
     ObjectOutputStream oos=null;
     Person kelly=new Person();
     kelly.age=(int)(10*Math.random());
     kelly.name="xixo";
     kelly.married=true;
     try{
     fos=new FileOutputStream("object.txt",true);
     oos=new ObjectOutputStream(fos);
     oos.writeObject(kelly);
    
     fis=new FileInputStream("object.txt");
     ois=new ObjectInputStream(fis);
     Object o=null;
     Person p=null;
     while((o=ois.readObject())!=null){
     p=(Person)o;
     System.out.println ("name:"+p.name+"age:"+p.age+"married:"+p.married);
    
     }
    
     }catch(Exception e){
     e.printStackTrace();
    
     }finally{
     try{
     ois.close();
     oos.close();
     }catch(Exception e){
     e.printStackTrace();
    
     }
     }
    }
    
    
}
class Person implements Serializable{
public int age;
public String name;
public boolean married;

}

解决方案 »

  1.   

    说实话,我看完了java网络传输,还是没有体会序列化得好处,个人觉得不用它不是也一样
      

  2.   

    class Person implements Serializable{ 
    public int age; 
    public String name; 
    public boolean married; } 
     
     
     
    有Serializable接口啊,实现了阿
      

  3.   

    StreamCorruptedException - Control information in the stream is inconsistent.
    此种控制方式下readObject方法运行多次导致异常
    直接调一遍无需控制
      

  4.   

    序列化效率太低最好不要用
    import java.io.Serializable;
    import java.io.FileInputStream;
    import java.io.*;public class Test { public static void main(String[] args) { FileInputStream fis = null;
    FileOutputStream fos = null;
    ObjectInputStream ois = null;
    ObjectOutputStream oos = null;
    Person kelly = new Person();
    kelly.age = (int) (10 * Math.random());
    kelly.name = "xixo";
    kelly.married = true;
    try {
    fos = new FileOutputStream("object.txt");
    oos = new ObjectOutputStream(fos);
    oos.writeObject(kelly);
    oos.writeObject(kelly);
    oos.close(); fis = new FileInputStream("object.txt");
    ois = new ObjectInputStream(fis);
    Object o = null;
    Person p = null;
    int i = fis.available();
    while (i>0) {
    o = ois.readObject();
    p = (Person) o;
    System.out.println("name:" + p.name + "age:" + p.age
    + "married:" + p.married);
    i = fis.available();
    } } catch (Exception e) {
    e.printStackTrace(); } finally {
    try {
    ois.close();
    oos.close();
    } catch (Exception e) {
    e.printStackTrace(); }
    }
    }}class Person implements Serializable {
    public int age;
    public String name;
    public boolean married;}
      

  5.   

    while((o=ois.readObject())!=null){ 判断没起到作用
    我想你如果在oos.writeObject(kelly); 后面再加一句oos.writeObject(null);就好了 
      

  6.   

    写得没有这么麻烦吧,这是我以前写的一个序列化的工具类public class SerializableEntity { /**
    * String path  序列化出的实体文件保存路径
    * Object o     需要序列化的对象
    * return void
    */
    public static void writeEntity(String path, Object o){
    FileOutputStream fileOut = null;
    ObjectOutputStream objOut = null;

    try {
    fileOut = new FileOutputStream(path+"/DBObj.obj",true);
    objOut = new ObjectOutputStream(fileOut);
    objOut.writeObject(o);
    objOut.flush();
    objOut.close();
    fileOut.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /**
    * String path  序列化出的实体文件路径
    * return Object
    */
    public static Object readEntity(String path){
    Object o = null;
    try {
    FileInputStream fileIn = new FileInputStream(path);
    ObjectInputStream objIn = new ObjectInputStream(fileIn);
    o = objIn.readObject();
    objIn.close();
    fileIn.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    }

    return o;
    }
    }
    只要你传入的Object实现了Serializable接口就行了,不过要注意的就是版本问题
    java序列化存在一个版本问题,就是说你要序列化一个javaBean名叫A,序列化前后A的版本必须一致,不能在类中修改任何东西,否则是不能正确反序列化的。
      

  7.   

    while((o=ois.readObject())!=null){ 这个地方读第二个被序列化的对象时因为没读到就错了。
      

  8.   

    学习java有这么一条准则,就是看的越多越好,不见得学的越细越好,这个问题碰到了再去研究,没见过的知道怎么找到它就行不然你永远都在学java se(这个就够你学几年的了)
      

  9.   

    while((o=ois.readObject())!=null){ }是这里有问题,不能循环去读的。应该这样:
     while(fis.available()!=0){
        p=(Person)(ois.readObject());
        System.out.println ("name:"+p.name+"age:"+p.age+"married:"+p.married);
     }