What is the best way to fully read a stream of objects from a file in Java?

后端 未结 4 1411
星月不相逢
星月不相逢 2021-01-14 17:33

I\'m creating a potentially long log of objects and do not want to keep them all in memory before writing to a file, so I can\'t write a serialized collection of the objects

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-14 18:19

    Your solution seems fine. Just make sure you have a finally clause, where you close your stream.

    Alternatively, you can create an EOF object of yours, and add it at the end. Thus you can check if the currently read object is the EofObject, and break at that point.

提交回复
热议问题