• java.io.StreamCorruptedException: invalid type code: AC错误的解决方法


    问题描述:

    在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流对象,在向外读数据的时候第一次运行的时候不会报错,在第二次就会报java.io.StreamCorruptedException: invalid type code: AC错误。

    原因:

    在一个文件都有一个文件的头部和文件体。由于对多次使用FileOutputStream(文件名,true)构建的ObjectOutputStream对象向同一个文件写数据,在每次些数据的时候他都会向这个文件末尾先写入header在写入你要写的对象数据,在读取的时候遇到这个在文件体中的header就会报错。导致读出时,出现streamcorrput异常(流中的控制信息不一致)。
    只有续写会先写入header,不是续写的话,写入多个对象也只会写入一次文件头,在一次续写时即使写入多个对象,也只会写入一次文件头

    知识储备:
    每个文件都有文件的头部和文件体两部分
    在对象输出流(ObjectOutputStream)中有Protected方法writeStreamHeader():void,这个方法是专门来写文件的头部了。

    解决办法:所以这里要判断是不是第一次写文件,若是则写入头部,否则不写入。

    代码示例:

    import java.io.*
    public class MyObjectOutputStream extends ObjectOutputStream{
        public MyFileOutputStream(){
            super();
        }
        public MyFileOutputStream(OutputStream o){
            super(o);
    }
        public void writeStreamHeader(){}//这里覆写父类中的方法,使他调用writeObject()的时候不写入文件头
    }
    
    class Demo{
        public static void main(String[] args){
            File file=new File(xxxxxxx);
            ObjectOutputStream out;
            //判断文件是否存在,以决定要使用哪个objectoutputstream
            if(file.isFile()){
            out=new MyObjectOutputStream(xxxx);
    }
                else{
                    out=new ObjectOutputStream(xxxxx);
    }
    }
    }
  • 相关阅读:
    六、TreeSet中的定制排序
    五、Set接口
    四、List接口
    Vocabulary Recitation 2020/05/20
    Vocabulary Recitation 2020/05/19
    Vocabulary Recitation 2020/05/18
    自控力_第六章
    Vocabulary Recitation 2020/05/17
    Vocabulary Recitation 2020/05/15
    Vocabulary Recitation 2020/05/14
  • 原文地址:https://www.cnblogs.com/wewill/p/5588755.html
Copyright © 2020-2023  润新知