多次创建object 写入对象文件中.java.io.StreamCorruptedException: invalid type code: AC解决办法

多次创建ObjectOutputStream对象写对象的话,会重复写入header,导致读出时,出现streamcorrput异常,所以这里要判断是不是第一次写文件,若是则写入头部,否则不写入。


多次写对象的文件内容:

 t str0t str1t str2 t str0t str1t str2

出现两个header信息( |),在读出时候就会报错:java.io.StreamCorruptedException: invalid type code: AC

 

示例:

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 ObjectOldWriter {
 public static void main(String[] args) {
          String filename = "d://mydata.data";
          oldWriter(filename);
          oldWriter(filename);
          oldReader(filename, 6);
 }

 public static void oldWriter(String filename) {
  File file = null;
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  try {
   file = new File(filename);
   fos = new FileOutputStream(file, true);
   oos = new ObjectOutputStream(fos);
   for(int i=0;i<3;i++){
    oos.writeObject("str"+i);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    if(fos!=null) fos.close();
    if(oos!=null) oos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void oldReader(String filename,int count) {
  FileInputStream fis = null;
  ObjectInputStream ois = null;
  try{
   fis = new FileInputStream(filename);
   ois = new ObjectInputStream(fis);
   for(int i=0;i<count;i++){
    System.out.println(ois.readObject());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }finally{
   try {
    if(fis!=null) fis.close();
    if(ois!=null) ois.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}
运行结果(报错):

str0
str1
str2
java.io.StreamCorruptedException: invalid type code: AC
 at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
 at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
 at com.effective.java.ObjectOldWriter.oldReader(ObjectOldWriter.java:51)
 at com.effective.java.ObjectOldWriter.main(ObjectOldWriter.java:16)

 

解决方法:

1.用同一个ObjectOutputStream写对象

2.但是大部分时候,要不断往某个文件记录对象,这样按照1的说法就要维护一个ObjectOutputStream,但是重启应用时候就会重新创建一个ObjectOutputStream对象,此时如果还是想往刚才那个文件里写对象的话,就会追加一个header。这样在读对象时读到这个位置就会报错。

解决方法是重写ObjectOutputStream的writeStreamHeader()方法

 

示例:

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;
import java.io.OutputStream;

public class ObjectNewWriter {
 ObjectNewWriter(){
  
 }
 public static void main(String[] args) {
          String filename = "d://mydata.data";
          newWriter(filename);
          newWriter(filename);
          newReader(filename, 6);
 }

 public static void newWriter(String filename) {
  File file = null;
  FileOutputStream fos = null;
  ObjectOutputStream oos = null;
  try {
   file = new File(filename);
   fos = new FileOutputStream(file, true);
            if(file.length()<1){
             oos = new ObjectOutputStream(fos);
            }else{
             oos = new MyObjectOutputStream(fos);
            }
   for(int i=0;i<3;i++){
    oos.writeObject("str"+i);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    if(fos!=null) fos.close();
    if(oos!=null) oos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
 public static void newReader(String filename,int count) {
  FileInputStream fis = null;
  ObjectInputStream ois = null;
  try{
   fis = new FileInputStream(filename);
   ois = new ObjectInputStream(fis);
   for(int i=0;i<count;i++){
    System.out.println(ois.readObject());
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }finally{
   try {
    if(fis!=null) fis.close();
    if(ois!=null) ois.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 
}
class MyObjectOutputStream extends ObjectOutputStream{

 public MyObjectOutputStream() throws IOException {
  super();
 }
 
 public MyObjectOutputStream(OutputStream out) throws IOException {
  super(out);
 }

 @Override
 protected void writeStreamHeader() throws IOException {
  return;
 
}

记住要删除你D盘下的mydata.data再执行该类

运行结果:

str0
str1
str2
str0
str1
str2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值