3 Replies Latest reply: Nov 23, 2008 8:58 AM by 800322 RSS

    Is there any way out to iterate with ObjectInputStream object?

    807589
      The following example is having three classes:

      1) Employee
      2)WriteObject
      3) ReadObject

      With ReadObject I was unable to put iteration to read the objects 1 by 1.
      Can ne1 help me out in this....

      ________________________________________
      import java.io.*;

      class Employee implements Serializable
      {
      private String code;
      private String name;

      public Employee(String code, String name)
      {
      this.code=code;
      this.name=name;
      }
      public String toString()
      {
      return code+" "+name;
      }
      }

      ------------------------------------------------------------------------------------------------
      import java.io.*;
      class WriteObject
      {
      public static void main(String s[]) throws Exception
      {
      FileOutputStream fos=new FileOutputStream("Empl.txt");
      ObjectOutputStream oos=new ObjectOutputStream(fos);
      Employee e=new Employee("A001","Ravish");
      oos.writeObject(e);
      e=new Employee("A002","Ram");
      oos.writeObject(e);

      }
      }


      _____________________________

      import java.io.*;

      class ReadObject
      {
      public static void main(String s[]) throws Exception
      {

      Employee e=null;
      FileInputStream fis=new FileInputStream("Empl.txt");
      ObjectInputStream ois=new ObjectInputStream(fis);


      *// If I want to put iteration here, how can I write that?*+

      e=(Employee)ois.readObject();
      System.out.println(e);
      e=(Employee)ois.readObject();
      System.out.println(e);


      }
      }


      thanx in advance.....