ObjectOutputStream抛出StreamCorruptedException异常

22 篇文章 0 订阅
4 篇文章 0 订阅

问题如下:

import java.util.*;  
import java.io.*;  
public class cpm {  
    public static void main(String[] args) {  
        /*
         * 这一块是存对象的,首先调用3次。
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("E:/a.dat",true));
            for(int i = 0; i < 10; i++) {
                oos.writeObject(new A(i,i));
            }
            oos.close();
        } catch(Exception ex){
            System.out.println(ex);
        }
        */


       /*存完对象之后,为什么调用这个读取只是读取出一组10个对象,而不是30个??
       ObjectInputStream ois = null;
       try {
           ois = new ObjectInputStream(new FileInputStream("E:/a.dat"));
           while(true) {
               A temp = (A)ois.readObject();
               temp.show();
            }
        } catch(Exception ex) {}
       finally {
           try {
               ois.close();
            }catch(Exception ex){}
        }
        */
    }
}


解决方法有点奇怪, 首先要继承ObjectOutputStream类写一个子类,覆盖writeStreamHeader()方法,完整的代码如下:

package sdfg.drfg;

import java.text.Collator;  
import java.util.*;  
import java.io.*;  
public class cpm {  
    public static void main(String[] args) throws Exception {  
        // TODO Auto-generated method stub  
         //* 这一块是存对象的,首先调用3次。
        MyObjectOutputStream oos = null;
        try {
            oos = MyObjectOutputStream.getInstance(new File("E:/a.txt"),true);
            
            for(int i = 0; i < 2; i++) {
                oos.writeObject(new A(i,i));
            }
            
            oos.writeObject(new A(5,6));
            oos.writeObject(new A(7,6));
            oos.close();
        } catch(Exception ex){
            ex.printStackTrace();
        }
        
       
       //存完对象之后,为什么调用这个读取只是读取出一组10个对象,而不是30个??
       ObjectInputStream ois = null;
       try {
           ois = new ObjectInputStream(new FileInputStream("E:/a.txt"));
           while(true) {
               A temp = (A)ois.readObject();
               temp.show();
            }
        } catch(Exception ex) {
            System.out.println(ex);
        }
       finally {
           try {
               ois.close();
            }catch(Exception ex){}
        }
        
    }
}

class A implements Serializable {
    private int x;
    private int y;
    public A(int x,int y) {
        this.x = x;
        this.y = y;
    }
    public void show() {
        System.out.println(x + " " + y);
    }
}

class MyObjectOutputStream extends ObjectOutputStream {
    static File f;
    static boolean _append;
    private MyObjectOutputStream(File file,boolean append) throws IOException {
        super(new FileOutputStream(file,append));
    }
    
    protected void writeStreamHeader() throws IOException {
        if(!f.exists()||f.length()==0||_append==false) {
            super.writeStreamHeader();
        }
        else {
            super.reset();
        }
    }
    
    public static MyObjectOutputStream getInstance(File file,boolean append) {
        f = file;
        _append = append;
        try {
            return new MyObjectOutputStream(file,append);
        } catch(IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值