简体   繁体   中英

Storing Serializable object to file with some data excluded

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.

您应该将您不想序列化的属性标记为瞬态:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM