The _java of object serialization and deserialization in Java

Source: Internet
Author: User
Tags object serialization serialization

        Serialization (serialization) is the process of converting the state information of an object into a form that can be stored or transmitted. Typically, an object is stored in a storage medium, such as a file or a memory buffer. In the process of network transfer, it can be a byte or XML format. The byte or XML encoding format can restore objects that are completely equal. This reverse process is also known as deserialization . Serialization and deserialization of
Java objects
in Java, we can create objects in many ways, and we can reuse the object as long as the object is not recycled. However, all of the Java objects we create are in the JVM's heap memory. These objects may exist only if the JVM is in the running state. Once the JVM stops running, the state of these objects is lost.
But in real-world scenarios, we need to persist these objects and reread them as needed. Java object Serialization can help us implement this functionality. The
Object serialization mechanism (serialization) is an object persistence method built into the Java language that, through object serialization, can save the state of an object as a byte array, and then convert the byte array to an object in a deserialized manner when necessary. Object serialization makes it easy to convert between active objects and byte arrays (streams) in the JVM.
in Java, the serialization and deserialization of objects is widely applied to RMI (remote method invocation) and network transport.
related interfaces and classes
Java provides a convenient API to enable developers to serialize and deserialize Java objects. These include the following interfaces and classes:

    • Java.io.Serializable
    • Java.io.Externalizable
    • ObjectOutput
    • ObjectInput
    • ObjectOutputStream
    • ObjectInputStream
    • Serializable interface

Class enables its serialization functionality by implementing the Java.io.Serializable interface. A class that does not implement this interface will not be able to serialize or deserialize any of its states. All subtypes of a serializable class are themselves serializable. The serialization interface has no methods or fields and is used only to identify serializable semantics. (This interface does not have methods and fields, why only objects that implement the interface's classes can be serialized?) )
When an attempt is made to serialize an object, an object that does not support the Serializable interface is encountered. In this case, the notserializableexception is thrown.
If the class to be serialized has a parent class, the parent class should also integrate the Java.io.Serializable interface in order to persist the variables defined in the parent class at the same time.
The following is a class that implements the Java.io.Serializable interface

Package Com.hollischaung.serialization.SerializableDemos;
Import java.io.Serializable;
/**
* Created by Hollis on 16/2/17.
* Implement Serializable interface
/public class User1 implements Serializable {
private String name;
private int age;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
@Override public
String toString () {return
"user{" +
"name= '" + name + ' \ ' +
", age=" + Age +
" }';
}
}

Serialization and deserialization through the following code

Package Com.hollischaung.serialization.SerializableDemos;
Import Org.apache.commons.io.FileUtils;
Import Org.apache.commons.io.IOUtils;
Import java.io.*;
/** * Created by Hollis on 16/2/17. * SerializableDemo1 combines SERIALIZABLEDEMO2 to show that a class must implement the Serializable interface/public class SerializableDemo1 {public static V if it wants to be serialized
OID Main (string[] args) {//initializes the Object User1 user = new User1 (); User.setname ("Hollis"); User.setage (23);
SYSTEM.OUT.PRINTLN (user);
Write Obj to File objectoutputstream oos = null; try {oos = new ObjectOutputStream (New FileOutputStream ("Tempfile")); Oos.writeobject (user);} catch (IOException e) {E.P
Rintstacktrace ();
finally {ioutils.closequietly (oos);}//read Obj from File File = new file ("Tempfile");
ObjectInputStream ois = null;
try {ois = new ObjectInputStream (new FileInputStream (file));
User1 NewUser = (User1) ois.readobject ();
System.out.println (NewUser); catch (IOException e) {e.printstacktrace ();} catch (ClassNotFoundException e) {E.printstaCktrace (); 
finally {ioutils.closequietly (OIS); try {fileutils.forcedelete (file);} catch (IOException e) {e.printstacktrace ();} }}//output://user{name= ' Hollis ', age=23}//user{name= ' Hollis ', age=23}

The above is the entire content of this article, I hope to learn Java in the object serialization and deserialization help.

Related Article

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.