JAVA ObjectInputStream报 objectInputStream invalid type code: AC 解决方法的一些注意点

感谢原文作者:攻城狮_无名
原文链接:https://blog.csdn.net/mingyang_2016/article/details/75208117


⚠️个人对这个文章的补充

这里解决方法的第三个方法,我在测试的时候怎么就是不对,最后终于搞明白了,是因为,每次新的ObjectInputStream对象在第一次读取时,都会默认读取一个头(就是解决方法二种的那个头),但是呢,如果写入的时候是一次写入多个对象,则这些对象只有一个头,这就会导致,读取时,第二个ois对象没办法读到新的头,所以会报错java.io.StreamCorruptedException: invalid stream header: 7371007E。

  • 第三种方法适用情况
  • 第二种方式的错误用法

问题描述:

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

猜你喜欢

转载自blog.csdn.net/qq_20156289/article/details/108416887