将可序列化对象存储到文件中,并排除一些数据
我有一个对象,它允许我将 BufferedImage 存储到我的对象文件中。在同一个对象中,我有 BufferedImage 变量,用于在第一次从原始数据数组加载图像后缓存图像。当我创建对象并将其存储到文件时,一切正常,因为 BufferedImage 为空。当我更新加载的对象并且变量正在初始化并且我想在更新后保存对象时,问题就出现了。
是否有可能将可序列化对象存储到文件中,不包括某些变量?或者也许我可以在存储到文件时以某种方式重置我的 BufferedImage 变量?
提前致谢, 谢尔希。
I have an object which allows me to store BufferedImage into my object file. In the same object I have BufferedImage variable which I use to cache the image after it's loaded first time from raw data array. Everything works fine when I'm creating object and storing it to file, since BufferedImage is null. The problem comes when I'm updating loaded object and the variable is being initialized and I want to save the object after it's updated.
Is there any possibility to store the serializable object to file, excluding some of the variables? Or maybe I can reset somehow my BufferedImage variable when storing to file?
Thanks in advance,
Serhiy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将不想序列化的属性标记为瞬态:
you should mark the attribute you don't want to serialize as transient :
变量可以标记为
瞬态
。如果一个变量被标记为瞬态,那么当您的对象被序列化时,它就不会被序列化。此外,当您从存储状态检索对象时,瞬态变量将具有空值。因此,您需要确保在使用瞬态变量之前重新初始化它。
Variables can be marked as
transient
. If a variable is marked as transient it wont get serialized when your object is being serialized.Also when you retrieve the object from the stored state, the
transient
variable will be having a null value. So you need to make sure that you are re-initializing the transient variable before you use it.