Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use transient to de-serialize a property

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use transient to de-serialize attributes", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use transient to de-serialize attributes" bar!

Transient keyword

By default, when serialization of an object is performed, the contents of all properties in the class are fully serialized, but in many cases, some properties may not need to be serialized, so you can use the transient keyword on the property definition.

Private transient String name

The contents of the name attribute will not be saved during sequence processing, in other words, the read data name will be the default value of its corresponding data type "null".

Import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;@SuppressWarnings ("serial") class Person implements Serializable {/ / Person class can be serialized private transient String name;private int age;public Person (String name, int age) {this.name = name;this.age = age;} @ Overridepublic String toString () {return "name:" + this.name + ", age:" + this.age " }} public class JavaAPIDemo {private static final File SAVE_FILE=new File ("D:" + File.separator + "mldn.person"); public static void main (String [] args) throws Exception {/ / saveObject (new Person ("Little sneeze", 78)); System.out.println (loadObject ());} public static void saveObject (Object obj) throws Exception {ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (SAVE_FILE)); oos.writeObject (obj); / / serialize oos.close () } public static Object loadObject () throws Exception {ObjectInputStream ois=new ObjectInputStream (new FileInputStream (SAVE_FILE)); Object obj= ois.readObject (); / / deserialize ois.close (); return obj;}} / / name: null, age: 78

If you assume that there are some classes that need to calculate and save attribute contents that often do not need to be serialized, you can use transient at this time, but in actual development, most of the classes that need to be serialized are often simple java classes, so this keyword does not appear frequently.

Thank you for reading, the above is the content of "how to use transient to de-serialize attributes". After the study of this article, I believe you have a deeper understanding of how to use transient to detach attributes from serialization, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report