先来看看java的ObjectOutputStream和ObjectInputStream源码好了
public ObjectOutputStream(OutputStream out) throws IOException { verifySubclass(); bout = new BlockDataOutputStream(out); handles = new HandleTable(10, (float) 3.00); subs = new ReplaceTable(10, (float) 3.00); enableOverride = false; writeStreamHeader(); bout.setBlockDataMode(true); if (extendedDebugInfo) { debugInfoStack = new DebugTraceInfoStack(); } else { debugInfoStack = null; } }
public ObjectInputStream(InputStream in) throws IOException { verifySubclass(); bin = new BlockDataInputStream(in); handles = new HandleTable(10); vlist = new ValidationList(); enableOverride = false; readStreamHeader(); bin.setBlockDataMode(true); }
不难发现在序列化new的时候调用了writeStreamHeader()方法写入了4个字节的StreamHeader
但是呢在读取的时候只有读取一次StreamHeader的,所以导致出错
贴图:
这里我们可以看下StreamHeader的内容:
protected void writeStreamHeader() throws IOException { bout.writeShort(STREAM_MAGIC); bout.writeShort(STREAM_VERSION); }
很简单,一个魔数,一个版本号。两个都是short类型,占4个字节。
解决方法就是把StreamHeader给去掉就行了啦,使用以下方法:
public static void writeObj(File file, Object obj) throws IOException { boolean isexit = false; if (file.exists()) { isexit = true;// 序列化文件存在,追加内容 } FileOutputStream fileOutputStream = new FileOutputStream(file,true); // 每次new的时候都会写入一个StreamHeader,所以要把屁股后面的StreamHeader去掉 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); long pos = 0;// 可以说是文件的长度 if (isexit) { // getChannel()返回此通道的文件位置,这是一个非负整数,它计算从文件的开始到当前位置之间的字节数 pos = fileOutputStream.getChannel().position() - 4;// StreamHeader有4个字节所以减去 // 将此通道的文件截取为给定大小 fileOutputStream.getChannel().truncate(pos); System.out.println("追加成功~"); } objectOutputStream.writeObject(obj); // 关闭流 objectOutputStream.close(); fileOutputStream.close(); }
(反序列化)读取多个对象:
FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; java.util.List<Student> list = new ArrayList<Student>(); try { fileInputStream = new FileInputStream(file); objectInputStream = new ObjectInputStream(fileInputStream); while (fileInputStream.available() > 0) { list.add((Student) objectInputStream.readObject()); } System.out.println("读取~"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
参考博客:点击打开链接