I am reading objects from a file using Serializable:
public ArrayList<Object> deserialzePerson(String filename) {
Object obj = null;
ObjectInputStream ois;
try {
ois = new ObjectInputStream(new FileInputStream(filename));
for (int i = 0; i < 100; i++) {
obj = (Object) ois.readObject();
ObjectArray.add(obj);
}
} catch (Exception e) {
}
return ObjectArray;
}
However, I do not know the number of objects in the file and use the number "100" in the for-loop. If there are less than 100, the exception will begin, and everything will be as expected. However, I think this is a bad decision because it depends on fishing errors. Is there a way to set a limit on the loop for the number of objects in a file?
For example, when reading from a .txt file that I used .hasNext();, is there something similar for objects?
source
share