日期:2014-05-20 浏览次数:20680 次
package com.java.taskIO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test6 {
public static void main(String[] args) throws Exception {
List list = new ArrayList();
list.add(new Person("张三", 20));
list.add(new Person("李四", 21));
list.add(new Person("王五", 22));
list.add(new Person("赵六", 23));
OutputStream fos = new FileOutputStream(
"C:\\Users\\wd\\Desktop\\1.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Iterator it = list.iterator();
while (it.hasNext()) {
oos.writeObject(it.next());
}
InputStream is = new FileInputStream("C:\\Users\\wd\\Desktop\\1.txt");
ObjectInputStream ois = new ObjectInputStream(is);
Person p2 = (Person) ois.readObject();
System.out.println(p2);
oos.close();
ois.close();
}
}
我开始添加了4个对象,怎么才能一个一个的反序列化出来呢??我这样做只能现实第一个对象。