fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
Object object = ois.readObject();这个方法只能读出txt文件里面的第一条数据 而不能得到所有的数据 应该循环去读 但是这个怎么读呢 请教各位大侠
java.io.StreamCorruptedException: invalid type code: AC
obj=(Person) ois.readObject();
while(obj!=null){
obj = (Person) ois.readObject(); //循环下面的数据
list.add(obj);
}
Iterator it=list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
这么写有异常
你是不是定义了多次objectOutputStream对象?
追问// 把对象写入文件.
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(
"D:\\1\\a.txt", true));
ObjectInputStream ois = null;
try { ois = new ObjectInputStream(new FileInputStream(
"D:\\1\\a.txt"));
Student student=null;
while ((student=(Student) ois.readObject())!=null) {
list.add(student);