ObjectOutputStream抛出StreamCorruptedException异常

问题如下:

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;
    }
}


你可能感兴趣的:(java,j2se,IO,类)