A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 月光海 高级黑马   /  2014-4-15 15:09  /  1137 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. public static void read() throws Exception
  2.         {
  3.                 ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:\\a.txt"));
  4.                
  5.                 Person p=null;
  6.                 Object obj=null;
  7.                 while ((ois.readObject())!=null)
  8.                 {
  9.                         p=(Person)ois.readObject();
  10.                         System.out.println(p);
  11.                 }
  12.                 ois.close();       
  13.                
  14.         }
  15.         public static void write()throws IOException
  16.         {
  17.                 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:\\a.txt"));
  18.                 oos.writeObject(new Person("zhangsan",29));
  19.                 oos.writeObject(new Person("lisi",22));
  20.                 oos.writeObject(new Person("wangwu",39));
  21.                 oos.close();
  22.         }
复制代码
,我这样取报了个EOFException异常,为什么readObject()返回的不是object类型吗,如果后面没有了会返回什么类型,不是null吗?

评分

参与人数 1技术分 +1 收起 理由
黑妞~ + 1

查看全部评分

7 个回复

倒序浏览
读到最后没有了但你还继续读,就会给你个EOF  
回复 使用道具 举报
sheng6699 发表于 2014-4-15 15:28
读到最后没有了但你还继续读,就会给你个EOF

我知道在继续,所以我想知道读到最后没有对象了会返回什么类型,因为我while的条件就是在判断是不是到底了,很明显null是不行的,你能告诉我怎么判断吗?读到最后没有数据了返回的是什么呢?????????????????????????????
回复 使用道具 举报
你可以再文件结尾加个Null.来标示文件内容结束。
回复 使用道具 举报
这句   while ((ois.readObject())!=null) //如果后面没有了就会报eofException 就是end of file的简写嘛

  1. package Testpackage;


  2. import java.io.*;


  3. class Test{
  4.         public static void main(String args[]){
  5.                 try {
  6.                         write();
  7.                         read();
  8.                 } catch (Exception e) {
  9.                         // TODO Auto-generated catch block
  10.                         e.printStackTrace();
  11.                 }
  12.                
  13.                
  14.                
  15.                
  16.         }
  17. public static void read() throws Exception
  18.         {
  19.                 ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:\\a.txt"));
  20.                 Person p=null;
  21.                 Object obj=null;
  22.                 while ((ois.readObject())!=null)   //如果后面没有了就会报eofException 就是end of file的简写嘛
  23.                 {
  24.                         p=(Person)ois.readObject();
  25.                         System.out.println(p);
  26.                 }
  27.                 ois.close();
  28.                
  29.         }
  30.         public static void write()throws IOException
  31.         {
  32.                 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:\\a.txt"));
  33.                 oos.writeObject((Object)new Person("zhangsan",29));
  34.                 oos.writeObject((Object)new Person("lisi",22));
  35.                 oos.writeObject((Object)new Person("wangwu",39));
  36.                 oos.close();
  37.         }
  38. }

  39. class Person implements Serializable{
  40.         Person(String name,int i){}
  41. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
黑妞~ + 1

查看全部评分

回复 使用道具 举报
月光海 发表于 2014-4-15 15:34
我知道在继续,所以我想知道读到最后没有对象了会返回什么类型,因为我while的条件就是在判断是不是到底 ...

因为我while的条件就是在判断是不是到底了

既然你想判断是不是到底了,你直接捕捉一下这个Exception不行么。。:L
回复 使用道具 举报
Kelvinhu 发表于 2014-4-15 15:41
因为我while的条件就是在判断是不是到底了

既然你想判断是不是到底了,你直接捕捉一下这个Exception不行 ...

我试着捕捉过了,执行的第一遍其实就已经报异常了,不知道为什么
回复 使用道具 举报
  1. public static void read() throws Exception
  2.     {
  3.                 ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:\\a.txt"));
  4.                
  5.                 Person p=null;
  6.                 Object obj=null;
  7.                                 try
  8.                                 {
  9.                                         while ((ois.readObject())!=null)
  10.                                         {
  11.                         p=(Person)(ois.readObject());
  12.                         System.out.println(p);
  13.                                         }
  14.                                 }
  15.                                 catch (EOFException e)
  16.                                 {
  17.                                         System.out.println("over");
  18.                                 }
  19.                
  20.                 ois.close();
复制代码
这样就可以了!

评分

参与人数 1技术分 +1 收起 理由
黑妞~ + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马