1) to make an object a serialized object, this class implements the Serializable or Externalizable interface (Externalizable interface inheritance and serializable interface).
This serialization only serializes the object's non-transient instance variables , does not serialize the static member variables, and does not serialize the method
2)
Object serialization: The process of translating a Java "object" into a "byte sequence" is called object serialization
Object deserialization: The process of reverting a "byte sequence" to a Java "object" is called deserialization
Does not invoke any of the constructor methods of the class when deserializing
3)
Function description for object serialization:
1. Convert objects into bytes to the hard disk, usually stored in a file, when you open the file is a bit garbled, but also can see the corresponding information
2. Passing the byte sequence of an object on the network
Specific examples:
[Java]View plain copy
- Import java.io.Serializable;
- Import Java.io.ObjectOutputStream;
- Import Java.io.FileOutputStream;
- Import java.io.*;
- Serialization and deserialization of objects
- Public class Serializabletest
- {
- public static void Main (string[] args)
- {
- Person p = new Person ("xiaoming",172.0);
- person P2 = new Person ("Little Red",165.0);
- Person P3 = new Person ("Floret",168.0);
- Try
- {
- FileOutputStream fos = new FileOutputStream ("E:/abc/person.txt");
- //Implement object serialization to write objects into a file in a byte-stream format
- ObjectOutputStream Oos = new ObjectOutputStream (FOS);
- //object serialization can only write "non-static member variable" to the stream, static member variable, or the method can not be written into the stream
- Oos.writeobject (P);
- Oos.writeobject (p2);
- Oos.writeobject (p3);
- Oos.close ();
- //Deserialize, write the object in the file back into the program (memory)
- FileInputStream FIS = new FileInputStream ("E:/abc/person.txt");
- ObjectInputStream ois = new ObjectInputStream (FIS);
- Person pe1 = (person) ois.readobject ();
- Person pe2 = (person) ois.readobject ();
- Person pe3 = (person) ois.readobject ();
- System.out.println (pe1.name+"," + Pe1.age + "," + pe1.height ");
- System.out.println (pe2.name+"," + Pe2.age + "," + pe2.height ");
- System.out.println (pe3.name+"," + Pe3.age + "," + pe3.height ");
- Ois.close ();
- }
- catch (Exception e)
- {
- E.printstacktrace ();
- }
- }
- }
- Class Person implements Serializable //The object implements the Serializable interface, which is a serialized object
- {
- String name;
- int age;
- double height;
- Public person (String name, int. Age, double height)
- {
- this.name = name;
- this.age = age;
- this.height = height;
- }
- }
If the user wants to control how the class is serialized, you can provide the following form of the WriteObject () and ReadObject () methods in the Serializable class, which you can refer to the API.
private void WriteObject (Java.io.ObjectOutputStream out) throws IOException
private void ReadObject (Java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
Serialization and deserialization of objects