Dark Horse programmer _ java study note _ Java serialization and deserialization

Source: Internet
Author: User

1. What is serialization?
Simply put, the state of various objects stored in the memory (that is, instance variables, not methods) can be read again. Although you can use your own methods to save object states, Java provides you with a mechanism better than your own to save the object state, that is, serialization.

2. Under what circumstances should serialization be performed?
A) when you want to save the object state in memory to a file or database;
B) when you want to use a socket to transmit objects over the network;
C) when you want to transmit objects through RMI;

 

Let's give another example in the jdk, so you should know how to use it:

Package book. io;


Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. ObjectInputStream;
Import java. io. ObjectOutputStream;
Import java. io. Serializable;
Import java. util. Date;
/**
* Serialization and deserialization objects
*/
Public class SerializeObject {
// An internal class for serialization and deserialization.
// Serializable must be implemented before serialization and deserialization can be performed.
Static class MyClass implements Serializable {
// General instance variables will be serialized and deserialized
Private int a, B;
// Transient instance variables will not be serialized or deserialized
Private transient int c;
// Class variables are not serialized or deserialized
Private static int d;
Public MyClass (){
}
Public MyClass (int a, int B, int c, int d ){
This. a =;
This. B = B;
This. c = c;
MyClass. d = d;
}
Public String toString (){
Return this. a + "" + this. B + "+ this. c +" + MyClass. d;
}
}

/**
* Serialize an object to a file
*/
Public static void serialize (String fileName) throws Exception {
// Create an object output stream to output the object to a file
ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream (fileName ));
// Serialize A String object to a file
Out. writeObject ("Today :");
// Serialize the object of the current date to the file
Out. writeObject (new Date ());
// Serialize A MyClass object
MyClass my1 = new MyClass (5, 6, 7, 8 );
Out. writeObject (my1 );
Out. close ();
}
/**
* Deserialization from files to objects
*/
Public static void deserialize (String fileName) throws Exception {
// Create an object input stream to read objects from a file
ObjectInputStream in = new ObjectInputStream (new FileInputStream (fileName ));
// Note that the read object must be read in the sequence when the object is serialized; otherwise, an error occurs.
// Read the string object
String today = (String) (in. readObject ());
System. out. println (today );
// Read date object
Date date = (Date) (in. readObject ());
System. out. println (date. toString ());
// Read the MyClass object and call its add method.
MyClass my1 = (MyClass) (in. readObject ());
System. out. println (my1.toString ());
In. close ();
// When the object is restored, all fields in the object are automatically restored. If you do not want a domain to be serialized
// Add the transient keyword, for example, the following code: transient int noSer = 0;
// Similarly, if a field in the class is static, it will not be serialized.
}
/**
* @ Param args
*/
Public static void main (String [] args) throws Exception {
String fileName = "c:/temp/MyClass. ser ";
SerializeObject. serialize (fileName );
// Comment out the second line and run only the following line. The output is different.
SerializeObject. deserialize (fileName );
}
}

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.