- package com.ccus.test;
- 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 TestPerson {
- public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
- read();
- }
- public static void read() throws FileNotFoundException, IOException, ClassNotFoundException
- {
- ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\test\\obj.txt"));
- Person p = null;
- while((p=(Person)ois.readObject()) != null)//会报错
- {
- System.out.println(p);
- }
- System.out.println(p);
- }
- public static void write() throws FileNotFoundException, IOException
- {
- Person p = new Person("叶立民",20,"男");
- Person p1 = new Person("叶虎",30,"男");
- ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\test\\obj.txt"));
- oos.writeObject(p);
- oos.writeObject(p1);
- oos.close();
- }
- }
复制代码 在毕老师的视频中,我们通过序列化存入一个对象到硬盘上,并且可以再次读出来,但是当我们存储多个对象到一个文件,可以吗?然后怎么读出来??,我试着可以存储多个对象进入文件,而且可以读出来,但是读出来后报错,ObjectInputStream读取Object以什么来判断结束??求大神指导
|
|