Java ObjectOutputStream and updating a file

前端 未结 1 2017
我在风中等你
我在风中等你 2021-01-28 23:31

I am having trouble figuring out one implementation problem, I have one class, it behaves like list but instead of holding a file in some collection it saves them on a disk.

相关标签:
1条回答
  • 2021-01-29 00:20

    Typically with stream based classes (especially higher order streams like OOS), you should rewrite the whole file, anytime you update it.

    If you really INSIST on only updating part of a file, then you should think of the file as made up of N streams, where each 'stream' represents one object that you are writing. So i would use a RandomAccessFile for the base file, and then when i want to write an object i would wrap an ObjectOutputStream on top of a ByteArrayOutputStream, write your object into that, then take those bytes, and rewrite those bytes into the RandomAcessFile where you want.

    This probably won't be particularly efficient, as you will write N OOS headers, and N class descriptions for the object you are writing.

    0 讨论(0)
提交回复
热议问题