作业的大致要求是,定义两个方法,一个方法用ObjectOutputStream流写入几个对象(每次只写一个对象),另外一个方法读取几个对象,返回一个。
碰到的问题01:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
这个真的是难到我了,解决了很久,都没有解决,然后就上网看了下,
The underlying problem is that you are using a new
ObjectOutputStream
to write to an existing
ObjectInputStream
that you have already used a prior
ObjectOutputStream
to write to. These streams have headers which are written and read by the respective constructors, so if you create another
ObjectOutputStream
you will write a new header, which starts with - guess what? -
0xAC,
and the existing
ObjectInputStream
isn't expecting another header at this point so it barfs.
大致的意思是,每次用一个新的ObjectOutputStream写入文件中去,会写入一个新的header,所以在读的时候回出错,老师还带了我们看了下源码有一个方法writeStreamHeader(),每一个新建的都会写头,所以会出错。
解决办法:
01.这个ObjectOutputStream定义成一个全局的变量,然后在main方法中初始化,然后就只有一次
02.新建一个类继承ObjectOutputStream,然后覆写这个方法,让这个方法的方法体为空,在新建这个流的时候。判断文件的长度,如果是0,则新建的是标准的ObjecctOutputStream,如果不是0,则新建的是自己定义的类,调用方法。
碰到的问题02:
Exception in thread "main"
java.io.EOFException
的长这个问题的原因就是读取的时候,由于之前学的流的经验。所以,然后又需要循环的读入对象
,
变态的认为结束的条件就是((obj = bis.readObject()) != null),导致抛出异常。
解决的办法,一开始还没有想出来,还是有时候思路太局限了。老师一说,直接捕捉这个异常,然后异常中处理的内容就是返回就行了,当时就觉得思想被局限了,这个方法还是知道的。但是就是没有想到,心里有点凉。
以下是完整的代码:
package com.qianfeng.ch08;import java.io.EOFException;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.Serializable;import java.util.ArrayList;import java.util.List;public class TestObjectStream {public static void main(String[] args) throws Exception {// text.txt ---text.txt.txt; FileNotFoundExceptionFile file = new File("file/Person.txt");List<Person> list = new ArrayList<Person>();list.add(new Person(1, "张三1"));list.add(new Person(2, "张三2"));list.add(new Person(3, "张三3"));list.add(new Person(4, "张三4"));// 保存for (int i = 0; i <list.size(); i++) {savePersonToFile(list.get(i), file);}//savePersonToFile(p1, file);List<Person> list_read=readPersonFormFile(file);for (Person person : list_read) {System.out.println(person);}}public static void savePersonToFile(Person p, File file) {ObjectOutputStream stream = null;try {if (file.length() <= 0) {stream = new ObjectOutputStream(new FileOutputStream(file, true));} else {stream = new MyObjectOutputStream(new FileOutputStream(file, true));}stream.writeObject(p);stream.close();} catch (Exception e) {e.printStackTrace();}}public static List<Person> readPersonFormFile(File file) {List<Person> list = new ArrayList<>();ObjectInputStream ois;try {ois = new ObjectInputStream(new FileInputStream(file));Person p = (Person) ois.readObject();while(p!=null){list.add(p);p=(Person) ois.readObject();}ois.close();} catch (EOFException e) {return list;} catch (ClassNotFoundException e) {e.printStackTrace();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return list;}}class MyObjectOutputStream extends ObjectOutputStream {protected MyObjectOutputStream(FileOutputStream fos) throws IOException, SecurityException {super(fos);}@Overrideprotected void writeStreamHeader() throws IOException {System.out.println("写了头");// super.writeStreamHeader();}}class Person implements Serializable {/****/private static final long serialVersionUID = 1L;private int id;private String name;public Person(int id, String name) {super();this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Person [id=" + id + ", name=" + name + "]";}}

被折叠的 条评论
为什么被折叠?



