Serialization and deserialization of Java objects _java

Source: Internet
Author: User

The example of this article for everyone to share the serialization and deserialization of Java objects for your reference, the specific contents are as follows

1. What is serialization
converting an object to a stream of bytes, such as saving it to a file, and then restoring the object at a later time, is called object serialization. (Add: Save the object to a permanent storage device called persistence)

2. How to achieve serialization
The serializable interface needs to be implemented, and the Java object implements this interface to show that the object of this class is serializable.

3. Considerations for serialization

(1) When an object is serialized, only non-static member variables of the object can be saved , and methods and static member variables cannot be saved.
(2) object A refers to object B, object A is serialized, and B is followed by serialization.
(3) If a serializable object contains a reference to an object that is not serializable, the entire serialization operation fails and the failure throws Notserializableexception, so Both the object and the Reference object implement the Serializable interface to serialize .
(4) the use of transient , member variables or references marked as transient, then the object can still be ordered to die, but will not be serialized into the file.

4. Code

 public class Person implements Serializable {private String name; 
  
 private transient int age; 
  Public person (String name, int age) {super (); 
  THIS.name = name; 
 This.age = age; 
 Public String toString () {return this.name + "-" + this.age; } 
} 
public class Serializableclient {public 
 
 static void Main (string[] args) throws Exception {person 
  p1 = new person ("Zhangsan", 5); 
  person P2 = new Person ("Lisi"); 
   
  Serialized 
  FileOutputStream fos = new FileOutputStream ("Person.txt"); 
  ObjectOutputStream oos = new ObjectOutputStream (FOS); 
   
  Oos.writeobject (p1); 
  Oos.writeobject (p2); 
   
  System.out.println ("---------"); 
   
  Deserialization of 
  FileInputStream fis = new FileInputStream ("Person.txt"); 
  ObjectInputStream ois = new ObjectInputStream (FIS); 
   
  for (int i = 0; i < 2; i++) {person 
   Tempperson = (person) ois.readobject (); 
   System.out.println (Tempperson); 
  } 
  


Output results:
---------
zhangsan-0
lisi-0
5. Finer-grained control serialization and deserialization

When we implement the above two private methods in a serialized or deserialized class (the method declaration is consistent with the above), we allow the process of serialization and deserialization to be controlled in a much lower, more granular way.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.