这句 while ((ois.readObject())!=null) //如果后面没有了就会报eofException 就是end of file的简写嘛
- package Testpackage;
- import java.io.*;
- class Test{
- public static void main(String args[]){
- try {
- write();
- read();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
-
-
- }
- public static void read() throws Exception
- {
- ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:\\a.txt"));
- Person p=null;
- Object obj=null;
- while ((ois.readObject())!=null) //如果后面没有了就会报eofException 就是end of file的简写嘛
- {
- p=(Person)ois.readObject();
- System.out.println(p);
- }
- ois.close();
-
- }
- public static void write()throws IOException
- {
- ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:\\a.txt"));
- oos.writeObject((Object)new Person("zhangsan",29));
- oos.writeObject((Object)new Person("lisi",22));
- oos.writeObject((Object)new Person("wangwu",39));
- oos.close();
- }
- }
- class Person implements Serializable{
- Person(String name,int i){}
- }
复制代码
|