How to count the number of objects stored in a * .ser file

I am trying to read all the objects stored in a * .ser file and store them in an array of objects. How can I get the number of objects stored in this file (so that I can declare an array of length number_of_objects)?

I checked the API and could not find the desired function.

-edit-
Part of the code:

Ser[] objTest2 = new Ser[number_of_objects];
for(int i=0; i<=number_of_objects, i++) {
    objTest2[i] = (Ser)testOS2.readObject();
    objTest2[i].printIt(); 
}
+5
source share
3 answers

What you want to see is class ArrayList.

This is basically a dynamically growing array.

You can add elements to it as follows:

ArrayList list = new ArrayList();
list.add(someObject);
list.add(anotherBoject);

The list will grow as new items are added to it. Therefore, you do not need to know the size in advance.

, toArray() .

Object[] arr = list.toArray(new Object[list.size()]);

Edit:
, :

List<Ser> objTest2 = new ArrayList<Ser>();
while (testOS2.available > 0) {
    Ser toAdd = ((Ser)testOS2.readObject());
    toAdd.printIt(); 
    objTest2.add(toAdd);
}

* , available() .

+3

, EOFException. . List , .

-1
  while(true)
 {

      try
      {
      Employee e=(Employee) ois.readObject();
      System.out.println("successfully deserialized.........showing details of object.");
      e.display();
      }

      catch(Exception e)
      {
          if(e instanceof java.io.EOFException)
          {
          System.out.println("All objects read and displayed");
          break;
          }
          else
          {
              System.out.println("Some Exception Occured.");
              e.printStackTrace();

          }
        }
 }
-1
source

All Articles