如何更新文件中包含的序列化HashMap?

Saf*_*fie 1 java serialization

我有一个包含序列化HashMap的文件,该序列包含MyObject类型的元素:

?? sr java.util.HashMap???`? F 
loadFactorI     thresholdxp?@     w      t (a54d88e06612d820bc3be72877c74f257b561b19sr  com.myproject.MyObject C?m?I?/ I  partitionL hashcodet Ljava/lang/String;L idt Ljava/lang/Long;L offsetq ~ L    timestampq ~ L topicq ~ xp    q ~ ppppx
Run Code Online (Sandbox Code Playgroud)

现在,我还有其他一些MyObject对象想要添加到该地图中。但是,我不想先读取映射并将其反序列化回内存中,然后对其进行更新,然后将整个更新的映射写回到文件中。如何以一种更有效的方式更新文件中的序列化?

Gho*_*ica 5

How would one update the serialization in the file in a more efficient way?

Basically by reverse engineering the binary protocol that Java uses when serializing objects into their binary representation. That would enable you to understand which elements in that binary blob would need to be updated in which way.

Other people have already done that, see here for example.

Anything else is just work. You sitting down and writing code.

Or you write the few lines of code that read in the existing files, and write out a new file with that map plus the other object you need in there.

You see, efficiency depends on the point of view:

  • 您认为使用二进制序列化对象对文件进行单个更新是否如此紧迫,以至于需要通过手动“修补”二进制文件来完成
  • 您是否认为花费数小时来学习基础二进制格式,正确更新其内容的效率更高?

这样做的唯一有效理由(我能想到)是:要确切地了解以下内容:二进制数据格式以及如何修补内容。但是即使那样,也可能有比“花费更多时间重新实现Java二进制序列化”更多的洞察力(在现实世界中具有实际价值)为您提供了更多的见解。

  • 如果您的地图太大,那么将其存储为序列化文件可能是错误的用例。将数据存储在数据库中。 (3认同)