Appending to an ObjectOutputStream

前端 未结 6 1572
再見小時候
再見小時候 2020-11-22 03:00

Is it not possible to append to an ObjectOutputStream?

I am trying to append to a list of objects. Following snippet is a function that is called whenev

6条回答
  •  温柔的废话
    2020-11-22 03:52

    As the API says, the ObjectOutputStream constructor writes the serialization stream header to the underlying stream. And this header is expected to be only once, in the beginning of the file. So calling

    new ObjectOutputStream(fos);
    

    multiple times on the FileOutputStream that refers to the same file will write the header multiple times and corrupt the file.

提交回复
热议问题