.java.io.StreamCorruptedException: invalid type code: AC解决办法
来源:互联网 发布:mac卸载pd 编辑:程序博客网 时间:2024/05/22 14:21
问题描述:
在向一个文件写入可序列化对象时,每次只想向文件的末尾添加一个可序列化的对象,于是使用了FileOutputStream(文件名,true)间接的构建了ObjectOutputStream流对象,在向外读数据的时候第一次运行的时候不会报错,在第二次就会报java.io.StreamCorruptedException: invalid type code: AC错误。
原因:
在一个文件都有一个文件的头部和文件体。由于对多次使用FileOutputStream(文件名,true)构建的ObjectOutputStream对象向同一个文件写数据,在每次些数据的时候他都会向这个文件末尾先写入header在写入你要写的对象数据,在读取的时候遇到这个在文件体中的header就会报错。导致读出时,出现streamcorrput异常。
解决办法:所以这里要判断是不是第一次写文件,若是则写入头部,否则不写入。
代码示例:
1.MyObjectOutputStream.java文件
1 import java.io.*;class MyObjectOutputStream extends ObjectOutputStream { 2 public MyObjectOutputStream() throws IOException { 3 super(); 4 } 5 public MyObjectOutputStream(OutputStream out) throws IOException { 6 super(out); 7 } 8 @Override protected void writeStreamHeader() throws IOException { 9 return;10 }11 }
2.ObjectSave.Java文件
1 import java.io.*; 2 import java.util.*; 3 public class ObjectSave { 4 /** * @param args 5 * * @throws IOException 6 * * @throws IOException 7 * @throws FileNotFoundException 8 * */ 9 public static void main(String[] args) { 10 ObjectOutputStream out = null; 11 ObjectInputStream in = null;12 List<User> list = new ArrayList<User>();13 list.add(new User("admin", "admin", "123", 1)); 14 list.add(new User("zhang", "zhang", "123", 0));15 String path = "d://abc"; 16 try { //判断文件大小并调用不同的方法 17 File file = new File(path); 18 FileOutputStream fos = new FileOutputStream(file, true); 19 if(file.length()<1){ 20 out = new ObjectOutputStream(fos); 21 }else{ 22 out = new MyObjectOutputStream(fos); 23 } 24 //out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path,true))); 25 //out.writeObject(Calendar.getInstance()); 26 //判断文件大小并调用不同的方法 27 for (int i = 0; i < list.size(); i++) { 28 out.writeObject(list.get(i)); 29 } 30 } catch (Exception ex) { 31 ex.printStackTrace(); 32 } finally { 33 try { 34 out.close(); 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } 38 } try { 39 in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));40 //Calendar date = (Calendar) in.readObject(); 41 //System.out.format("On %tA, %<tB %<te, %<tY:%n", date); 42 while (true) { 43 User user = (User) in.readObject(); 44 System.out.println(user.getName()); 45 } 46 } catch (EOFException e) { 47 48 } catch (Exception ex) { 49 ex.printStackTrace(); 50 } finally { 51 try { 52 in.close(); 53 } catch (IOException e) { 54 e.printStackTrace(); } 55 } 56 }57 } 58 }59 }60 }
- .java.io.StreamCorruptedException: invalid type code: AC解决办法
- .java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC
- java.io.StreamCorruptedException: invalid type code: AC
- java.io.StreamCorruptedException: invalid type code: AC异常
- java.io.StreamCorruptedException: invalid type code: AC错误的解决方法
- java.io.StreamCorruptedException: invalid type code: AC问题解决
- 多次创建object 写入对象文件中.java.io.StreamCorruptedException: invalid type code: AC解决办法
- java.io.StreamCorruptedException: invalid type code: AC异常和对象流锁机制问题
- 关于StreamCorruptedException: invalid type code: AC 异常
- tomcat8.0 启动报错 java.io.StreamCorruptedException: invalid type code: 3F
- 存储两次序列化对象,读取报错问题 java.io.StreamCorruptedException: invalid type code: A
- java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
- java.io.StreamCorruptedException: invalid stream header: ABACED00
- java.io.StreamCorruptedException: invalid stream header: 异常
- Breef introduction of QEMU main function (QEMU 主函数简介)
- android开机logo
- 浏览器HTTP_USER_AGENT汇总——Firefox、Chrome、IE9、IE8、IE7、IE6 以及手机浏览器
- linux命令3(文本处理相关)
- 开发语言类 -- C++
- .java.io.StreamCorruptedException: invalid type code: AC解决办法
- [OS X笔记]安装MacPorts
- 如何在遗留代码基础上开发
- iptables/netfilter全攻略详解
- DirectFB 源码解读之gfx硬件加速如何工作
- C++ 枚举类型的思考
- java 对象转型
- DirectFB 源码解读之外设输入处理流程
- yii 使用模块目录下的布局文件