Java-文件-序列化-java.io.StreamCorruptedException: invalid type code: AC

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

问题描述:

每次向一个文件中序列化对象时 ,每次只想向文件末尾追加对象,而不是覆盖,可以使用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;
    }
}

转载:
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/mingyang_2016/article/details/75208117

猜你喜欢

转载自blog.csdn.net/huiguo_/article/details/109014518