如何附加到现有的 java.io.ObjectStream?
至于现在,当我尝试附加对象时,我将得到java.io.StreamCorruptedException
。我在互联网上搜索了克服该问题的方法。到目前为止我找到的答案是这是不可能的。解决此问题的一种方法是将对象写入列表,然后将该列表写入文件。
但是每次添加新对象时我都必须覆盖该文件。看来这并不是加时赛的最优方案。
有没有办法将对象附加到现有对象流?
As for now I will get java.io.StreamCorruptedException
when I try to append an Object. I have searched the Internet for a way to overcome that. The answer I found so far is it can't be done. A way around this problem is to write the objects into a list and then write the list to the file.
But I have to overwrite that file everytime when I add new objects. It seems not to be the optimal solution in overtime.
Is there a way to append objects to an existing object stream?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这实际上很容易做到。当您添加到现有流时,您需要使用覆盖
writeStreamHeader
的 ObjectOutStream 子类,以便第二个标头不会写入文件中间。例如然后只需使用标准的 ObjectInputStream 来读取整个文件。
It is actually pretty easy to do. When you are adding to an existing stream you need to use a subclass of ObjectOutStream that overrides
writeStreamHeader
so that a second header is not written in the middle of the file. For exampleThen just use a standard ObjectInputStream to read the whole file.
我发现的关于这个主题的最好的文章是:
http://codify.flansite。 com/2009/11/java-serialization-appending-objects-to-an-existing-file/
覆盖 ObjectOutputStream 的“解决方案”完全是错误的。我刚刚完成了由此引起的错误的调查(浪费了宝贵的两天)。它不仅有时会损坏序列化文件,而且甚至能够在不抛出异常的情况下进行读取,并最终提供垃圾数据(混合字段)。对于那些不相信的人,我附加了一些暴露问题的代码:
正如该文章所述,您可以使用以下解决方案之一:
The best article I've found on this topic is:
http://codify.flansite.com/2009/11/java-serialization-appending-objects-to-an-existing-file/
The "solution" that overrides ObjectOutputStream is simply wrong. I've just finished investigating a bug that was caused by that (wasting two precious days). Not only that it would sometimes corrupt the serialized file but even managed to read without throwing exceptions and in the end providing garbage data (mixing fields). For those in disbelief, I'm attaching some code that exposes the problem:
As stated in that article, you can use one of the following solutions:
非常感谢 George Hategan 揭露代码问题。我也检查了一段时间。然后,它击中了我。如果您使用子类 ObjectOutputStream 并重写 writeStreamHeader() 方法来写入数据,您必须使用并行子类 ObjectInputStream 并重写 readStreamHeader() 方法来读取数据。当然,我们可以在写入和读取对象的不同实现之间进行曲折,但只要我们在写入/读取过程中使用相应的子类对 - 我们(希望)会很好。汤姆.
Many thanks to George Hategan for the problem exposing code. I examined it for a while too. Then, it hit me. If you're using a sub-classed ObjectOutputStream with an override of the writeStreamHeader() method to write data, you must use the parallel sub-classed ObjectInputStream with an override of the readStreamHeader() method to read the data. Of course, we can zig-zag between different implementations of writing and reading objects, but as long as we use the corresponding pairs of sub-classes in the write/read process - we'll be (hopefully) fine. Tom.
您需要创建一个新的
ObjectInputStream
来匹配每个ObjectOutputStream
。我不知道如何将状态从完整的ObjectInputStream
传输到ObjectOutputStream
(无需完整的重新实现,无论如何,这在纯 Java 中有点棘手)。You would need to create a new
ObjectInputStream
to match everyObjectOutputStream
. I don't know a way to transfer state from a completeObjectInputStream
to anObjectOutputStream
(without a complete reimplementation, which is a bit tricky in pure Java anyway).