How to write and read java serialized objects into a file

前端 未结 5 1915
既然无缘
既然无缘 2020-11-27 03:13

I am going to write multiple objects to a file and then retrieve them in another part of my code. My code has no error, but it is not working properly. Could you please help

5条回答
  •  死守一世寂寞
    2020-11-27 03:55

    Why not serialize the whole list at once?

    FileOutputStream fout = new FileOutputStream("G:\\address.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fout);
    oos.writeObject(MyClassList);
    

    Assuming, of course, that MyClassList is an ArrayList or LinkedList, or another Serializable collection.

    In the case of reading it back, in your code you ready only one item, there is no loop to gather all the item written.

提交回复
热议问题