How to read serialized objects from file into ArrayList?

  • 0
    I write objects to a file through a loop without problems, but I don’t know how to read them back, because after a new program start, we don’t know how many objects are in the file.
    Java Anonymous, Aug 16, 2020

  • 1 Answers
  • 0
    If you are serializing 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());
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!