IO流反序列化(objectInputStream)和序列化(ObjectOutputStream)连续存储对象StreamCorruptedException

在做对象序列化和反序列化的时候最好可以做到整存整取,即不要追加。

如果追加会写入头部信息(比如每次写入都会加AC ED 00 05这样的信息),存进去时会变成:AC ED 00 05+存的信息+AC ED 00 05+存的信息..........而程序在反序列化读取时只读一次头部信息,即第一次的,后面的头部信息会被认为是内容,因此会报SreamCorruptedException错误。

第一种:整存整取,即所有对象先在内存(数组、集合)操作好了,再序列化到本地文件,反序列化读取也是整个读取到内存,再在内存中进行操作(增删改查),之后再序列化到本地文件会覆盖之前的文件内容。如此循环操作..........

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
 * 
  实现数据持久化的工具类
 * 
 */
public class FileUtils {
//实现数据的持久化
public static void saveDatas(Object cs) throws Exception{
//1.找到资源位置
File file  = new File("D:\\object.txt");
if(!file.exists()){
file.createNewFile();
}
//2.创建序列化输出流对象
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
//3.数据的持久化操作
oos.writeObject(cs);
//4.关闭流
oos.close();
}


//数据的反序列化(从磁盘获取数据)
public static Object getDatas() throws Exception{
File file = new File("D:\\object.txt");
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
Object os = ois.readObject();
ois.close();
fis.close();
return os;
}
}


第二种:只有第一次写入头部信息,在追加写入时不写入头部信息。

自己定义一个MyObjectOutputStream类继承ObjectOutputStream重写ObjectOutputStream的writeStreamHeader()方法。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;


public class MyOutputStream extends ObjectOutputStream {  
 
    public MyOutputStream(OutputStream out) throws IOException {  
        super(out);// 会调用writeStreamHeader()   
    }  
  
    /** 
     * 覆盖父类的方法,使其在已有对象信息并追加时,不写header信息 
     * 查看源码会发现:writeStreamHeader方法会写入以下两行内容: 
     *  
     *  bout.writeShort(STREAM_MAGIC); 
     *  bout.writeShort(STREAM_VERSION); 
     *   
     *  这两行对应的值: 
     *  final static short STREAM_MAGIC = (short)0xaced; 
     *  final static short STREAM_VERSION = 5; 
     *   
     *  在文件中头部就会写入:AC ED 00 05 
     *  一个文件对象只有在文件头出应该出现此信息,文件内容中不能出现此信息,否则会导致读取错误 
     *  所以在追加时,就需要覆盖父类的writeStreamHeader方法,执行reset()方法 
     *   
     *  reset()方法写入的是这个:final static byte TC_RESET =        (byte)0x79; 
     * @throws IOException 
     */  
    @Override  
    protected void writeStreamHeader() throws IOException {  
        super.reset();  
    }  

      
    public static ObjectOutputStream newInstance(String file) throws IOException {  
        File f = new File(file);  
        long length = f.length();  
        ObjectOutputStream oos = null;  
        if(f.length() == 0) {  
            oos = new ObjectOutputStream(new FileOutputStream(new File(file),true));  
        } else {  
            oos = new MyOutputStream(new FileOutputStream(new File(file),true));  
        }  

        return oos;  
    }  
}

//测试类

import javax.script.ScriptException;  
import java.io.*;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
/** 
 * Created by xjl456852 on 2016/3/11. 
 */  
public class ObjectTest {  
    /** 
     * 使用ObjectOutputStream写Object对象,不追加的情况,这个是可以的. 
     * @throws ScriptException 
     * @throws IOException 
     */  
    public void testObject() throws ScriptException, IOException {  
        ObjectOutputStream oos = null;  
        ObjectInputStream ois = null;  
        try {  
            String file = "d:\\object.txt";  
            //使用ObjectOutputStream追加写入,在多次写完之后,读取object信息时会有异常.所以不能这样写.  
//            oos = new ObjectOutputStream(new FileOutputStream(new File(file),true));  
            oos = new ObjectOutputStream(new FileOutputStream(new File(file)));  
              
            List<Map<Integer,String>> list = new ArrayList<Map<Integer,String>>();  
            Map<Integer, String> map = new HashMap<>();  
            map.put(1, "xjl");  
            map.put(2, "xjl");  
            list.add(map);  
            oos.writeObject(list);  
            oos.flush();  
              
            List<Map<Integer,String>> list2 = new ArrayList<Map<Integer,String>>();  
            Map<Integer, String> map2 = new HashMap<>();  
            map2.put(3, "xjl3");  
            map2.put(4, "xjl4");  
            list2.add(map2);  
            oos.writeObject(list2);  
            oos.flush();  
              
            ois = new ObjectInputStream(new FileInputStream(new File(file)));  
            List<Map<Integer,String>> listResult = (List<Map<Integer,String>>)ois.readObject();  
            List<Map<Integer,String>> listResult2 = (List<Map<Integer,String>>)ois.readObject();  
              
            printList(listResult);  
            printList(listResult2);  
              
              
              
        } catch(Exception e) {  
            e.printStackTrace();  
        } finally {  
            if(oos != null) {  
                oos.close();  
                oos = null;  
            }  
            if(ois != null) {  
                ois.close();  
                ois = null;  
            }  
        }  
    }  
  
    /** 
     * 使用自定义的方式,在同一个文件中追加Object,要使用下面的方式 
     * @throws ScriptException 
     * @throws IOException 
     */  
    public void testObject2() throws ScriptException, IOException {  
         
        ObjectInputStream ois = null;  
        try {  
            String file = "d:\\object.txt";  
  
            List<Map<Integer,String>> list = new ArrayList<Map<Integer,String>>();  
            Map<Integer, String> map = new HashMap<>();  
            map.put(1, "xjl");  
            map.put(2, "xjl");  
            list.add(map);  
            writeObject(list,file);  
  
            List<Map<Integer,String>> list2 = new ArrayList<Map<Integer,String>>();  
            Map<Integer, String> map2 = new HashMap<>();  
            map2.put(3, "xjl3");  
            map2.put(4, "xjl4");  
            map2.put(5, "xjl4");  
            list2.add(map2);  
            writeObject(list2,file);  
             
  
            ois = new ObjectInputStream(new FileInputStream(new File(file)));  
            List<Map<Integer,String>> listResult = (List<Map<Integer,String>>)ois.readObject();  
            List<Map<Integer,String>> listResult2 = (List<Map<Integer,String>>)ois.readObject();  
  
            printList(listResult);  
            printList(listResult2);  
  
        } catch(Exception e) {  
            e.printStackTrace();  
        } finally {  
              
            if(ois != null) {  
                ois.close();  
                ois = null;  
            }  
        }  
    }  
  
    /** 
     *  
     *  
     * 如果在文件中追加对象,每次创建ObjectOutputStream,都会在每次写入时增加header信息. 
     * 导致读取时出现java.io.StreamCorruptedException: invalid type code: AC异常 
     * @param t 
     * @param file 
     * @param <T> 
     * @throws IOException 
     */  
    public <T>void writeObject (T t, String file) throws IOException {  
         
        ObjectOutputStream oos = null;  
        try {  
            oos = MyOutputStream.newInstance(file);  
            oos.writeObject(t);  
            oos.flush();  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if(oos != null) {  
                oos.close();  
                oos = null;  
            }  
        }  
          
    }  
      
    public static void printList(List<Map<Integer, String>> list) {  
        for(Map<Integer,String> map : list) {  
            for(Map.Entry<Integer,String > entry : map.entrySet()) {  
                System.out.println(entry.getKey() +":"+ entry.getValue());  
            }  
        }  
    }  
}  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值