gpt4 book ai didi

java - ObjectOutputStream 状态机?

转载 作者:行者123 更新时间:2023-12-02 06:38:47 35 4
gpt4 key购买 nike

有没有办法强制 ObjectOutputStream 不依赖以前的写入,应该每次都写入对象属性和完整信息?

我可以从二进制文件中看到,它只写入以前版本的某种增量,如果您尝试加载随机项,它会失败?

最佳答案

是的,你想要ObjectOutputStream.reset()

public void reset() throws IOException

Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

之前,Javadoc 讨论了编写对已经见过的对象的引用:

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

正如 reset Javadoc 所解释的,调用 reset 会导致流状态重置。您应该知道,此重置操作本身会将字节写入流(以便解码端也知道何时重置流)。

关于java - ObjectOutputStream 状态机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19361764/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com