Simple JAVA serialization instance

Source: Internet
Author: User

1. Explanation of JAVA serialization in java api 1.6.0:
Class to enable its serialization function by implementing the java. io. Serializable interface. Classes that do not implement this interface cannot be serialized or deserialized in any State. All sub-types of serializable classes are themselves serializable. The serialization interface has no methods or fields and is only used to identify the serializable semantics.
To allow serialization of subtypes of non-serializable classes, assume that this subtype is responsible for saving and recovering ultra-type public, protected, and (if accessible) the status of the package field. Only when the child type extension class has an accessible non-parameter constructor to initialize the state of the class can it be assumed that the child type has this responsibility. If this is not the case, it is wrong to declare a class as a serializable class. This error will be detected at runtime.
During deserialization, fields of the non-serializable class will be initialized using the public or protected non-parameter constructor of this class. The serializable subclass must be able to access the non-parameter constructor. The field of the serializable subclass will be restored from the stream.
When traversing a graph, you may encounter objects that do not support the Serializable interface. In this case, NotSerializableException will be thrown and classes that identify non-serializable objects will be thrown.
2. Simple implementation of JAVA serialization:
A. First define a serialized Student Class
Package com. neusoft. test1;
Import java. io. Serializable;
Class Student implements Serializable {// defines a class that can be serialized
String name;
Int age;
String num;
Double score;
Public Student (){
}
Public Student (String name, int age, String num, double score ){
This. name = name;
This. age = age;
This. num = num;
This. score = score;
}
Public String toString (){
Return name + "\ t" + age + "\ t" + num + "\ t" + score;
}
}
B. Define a class to serialize the Student object
Package com. neusoft. test1;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. ObjectOutputStream;
Public class SerializableTest {
Public static void main (String [] args ){
Student stu_1 = new Student ("wjl", 25, "47843847", 89); // instantiate two serializable student objects
Student stu_2 = new Student ("yxm", 23," 47856547 ", 99 );
File f = new File ("c:/demo/456.txt"); // Save the object of the two objects
Try {
FileOutputStream fos = new FileOutputStream (f );
ObjectOutputStream oos = new ObjectOutputStream (fos );
System. out. println ("objects not serialized are as follows :");
System. out. println (stu_1 );
System. out. println (stu_2 );
Oos. writeObject (stu_1 );
Oos. writeObject (stu_2 );
System. out. println ("serialization successful !! ");
Oos. flush ();
Fos. close ();
Oos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
C. Define a class for deserializing the Student object
Package com. neusoft. test1;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
Import java. io. ObjectInputStream;
Public class TurnSerializableTest {
Public static void main (String [] args ){
File f = new File ("c:/demo/456.txt ");
Try {
FileInputStream FCM = new FileInputStream (f );
ObjectInputStream ois = new ObjectInputStream (FCM );
Student stu_1;
Stu_1 = (Student) ois. readObject ();
System. out. println (stu_1 );
Student stu_2 = (Student) ois. readObject ();
System. out. println (stu_2 );
FCM. close ();
Ois. close ();
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
D. put these three classes in one package. First, execute the SerializableTest class to serialize the two Student objects and save them to the c:/demo/456.txt file. then execute the TurnSerializableTest class to convert c: the two Student objects stored in the/demo/456.txt file are deserialized.

 

This article is from "extreme scholar"

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.