开发者

Storing Serializable object to file with some data excluded

开发者 https://www.devze.com 2023-02-12 02:17 出处:网络
I have an object which allows me to store BufferedImage into my object file. In the same 开发者_C百科object I have BufferedImage variable which I use to cache the image after it\'s loaded first time f

I have an object which allows me to store BufferedImage into my object file. In the same 开发者_C百科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.


you should mark the attribute you don't want to serialize as transient :

private transient BufferedImage image;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消