java.io.StreamCorruptedException: invalid type code: AC问题解决

来源:互联网 发布:安卓手机plc编程软件 编辑:程序博客网 时间:2024/06/07 00:29

问题描述:

每次向一个文件中序列化对象时 ,每次只想向文件末尾追加对象,而不是覆盖,可以使用FileInputStream(文件名,true);在读取数据的时候第一次会正常读取,不会报错,当读取第二次的时候,就会报出java.io.StreamCorruptedException: invalid type code: AC的错误。

问题分析:
由于用FileInputStream(文件名,true)向同一个文件中序列化对象,每次都会向文件中序列化一个header。在反序列化的时候每个 ObjectInputStream 对象只会读取一个header,那么当遇到第二个的时候就会报错,导致出现异常。

解决方案:

一共三种方法,推荐使用第二种;

一、运用集合:
在第一次序列化对象之前,把要序列化的对象添加到集合中,把这个集合序列化到文件中。然后每次序列化之前,除了把准备序列化的对象添加到集合中,再把已经序列化的集合反序列化回来添加到集合中,然后再把集合序列化到文件中。

二、重写ObjectOutputSream的writeStreamHeader()方法:
判断是不是第一次写入,若是则写入头部,若不是则不写入头部

/**重写writeStreamHeader()方法*/class MyObjectOutputStream  extends ObjectOutputStream{    public MyObjectOutputStream(OutputStream out) throws IOException {        super(out);    }    public void writeStreamHeader() throws IOException{        return;    }}public class SerializableDemo01{    //main方法    public static void main(String[] args) throws Exception {        File file = new File(".\\b.txt");        Person p = new Person("lisi",19);        set(file,p);        List<Person> list = get(file);        for(Person per:list){            System.out.println(per);        }    }    //序列化    public static void set(File file,Person p) throws Exception{        FileOutputStream fos = new FileOutputStream(file,true);        /**        判断是否是第一次写入        */        if(file.length()<1){            ObjectOutputStream oos = new ObjectOutputStream(fos);            oos.writeObject(p);            oos.close();        }else{            MyObjectOutputStream mos = new MyObjectOutputStream(fos);            mos.writeObject(p);            mos.close();        }    }    //反序列化    public static List<Person> get(File file) throws Exception{        List<Person> list = new ArrayList<Person>();        FileInputStream fis = new FileInputStream(file);        ObjectInputStream ois = new ObjectInputStream(fis);        while(fis.available()>0){            Person p = (Person) ois.readObject();            list.add(p);        }        ois.close();        return list;    }}

三:不重写ObjectOutputSream的writeStreamHeader()方法。在反序列化的while循环中,每次都创建一个新的ObjectInputStream用来读取header

public class SerializableDemo03{    public static void main(String[] args) throws Exception {        File file = new File(".\\c.txt");        Person p = new Person("lisi",19);        set(file,p);        List<Person> list = get(file);        for(Person per:list){            System.out.println(per);        }    }    public static void set(File file,Person p) throws Exception{        FileOutputStream fos = new FileOutputStream(file,true);        ObjectOutputStream oos = new ObjectOutputStream(fos);        oos.writeObject(p);        oos.close();    }    public static List<Person> get(File file) throws Exception{        List<Person> list = new ArrayList<Person>();        FileInputStream fis = new FileInputStream(file);        ObjectInputStream ois = null;        while(fis.available()>0){            //每次都new一个新的ObjectInputStream            ois = new ObjectInputStream(fis);            Person p = (Person) ois.readObject();            list.add(p);        }        ois.close();        return list;    }}
阅读全文
0 0
原创粉丝点击