I’m trying to read all the objects stored in a *.ser file and store them in a array of objects. How can I get the number of objects stored in that file(So that I can declare the array to be number_of_objects long)?
I’ve checked the API and was unable to find a Desirable function.
-edit-
A 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();
}
What you want to look at is the
ArrayListclass.It is basically a dynamically growing Array.
You can add items to it like so:
The list will grow as you add new items to it. So you don’t have to know the size ahead of time.
If you need to get an array out if the List at the end you can use the
toArray()method of List.Edit:
Here is a general implementation of what you need:
*I don’t think
available()is a reliable test for whether or not there are more bytes to read.