stu1=(ArrayList<Student>)ois.readObject();,每次到这里,就会直接跳到catch,而不会直接读取对象,但是在show方法里面就可以读取到?本人新手,求大佬指点,谢谢

解决方案 »

  1.   

    ArrayList<Student> stu1=null;
    Student stu=new Student(id, name, sex, classid, profession, birthday);
    try{
    //while (true) {
    stu1=(ArrayList<Student>)ois.readObject();
    //System.out.println(stu1.toString());
    stu1.add(stu);
    oos.writeObject(stu1);
    System.out.println("OK");
    //}
    }
    catch(EOFException e){
    stu1=new ArrayList<Student>();
    stu1.add(stu);
    oos.writeObject(stu1);
    System.out.println("OK"); }
    finally {
    fis.close();
    ois.close();
    }
      

  2.   

    public static void show() throws Exception{

    FileInputStream fis=new FileInputStream("Student.dat");
    ObjectInputStream ois=new ObjectInputStream(fis);

    try{
    while (true) {
    ArrayList<Student> stu1=(ArrayList<Student>)ois.readObject();
    System.out.println(stu1.toString());
    }
    }
    catch(EOFException e){

    }
    finally {
    fis.close();
    ois.close();
    }
    }