I
I
Igor Malyga2016-10-22 13:45:31
Java
Igor Malyga, 2016-10-22 13:45:31

How to read serialized objects from file into ArrayList?

I write objects to a file without problems through a loop, but I don’t know how to count them back, because after a new start of the program, we don’t know how many objects are in the file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Ne-Lexa, 2016-10-22
@SmallMiracle

If you are doing the serialization yourself, then before writing the objects, write down the length of the array.

data.writeInt(list.size());
for(Object o : list){
    data.writeXXX(o);
}

And read like this:
int size = in.readInt();
for(int i = 0; i < size; i++){
    list.add(in.readXXX());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question