
How to use the deserialization function in Java to achieve serialization and deserialization of objects
Serialization is to convert objects into objects that can be transmitted over the network or stored in The process of using a byte stream, and deserialization is the process of converting a byte stream back into an object. Java provides serialization and deserialization mechanisms so that developers can easily store and transmit objects. This article will introduce how to use the deserialization function in Java to serialize and deserialize objects, and give specific code examples.
- Implementing the Serializable interface
To implement serialization and deserialization of objects, you first need to ensure that the class to which the serialized object belongs implements the Serializable interface. This interface does not have any methods, it just serves as an identifier to tell Java that this class can be serialized.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
|
Copy after login
- Serialization of objects
To serialize an object into a byte stream, you can use the ObjectOutputStream class. First, you need to create a FileOutputStream object, pass it as a parameter to the constructor of ObjectOutputStream, and then use the writeObject method of ObjectOutputStream to write the object to the file.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class SerializationDemo {
public static void main(String[] args) {
Person person = new Person( "张三" , 25);
try {
FileOutputStream fileOut = new FileOutputStream( "person.ser" );
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(person);
out.close();
fileOut.close();
System.out.println( "对象已被序列化并保存在person.ser文件中" );
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
Copy after login
- Deserialization of objects
To deserialize a byte stream into an object, you can use the ObjectInputStream class. First, you need to create a FileInputStream object, pass it as a parameter to the constructor of ObjectInputStream, and then use the readObject method of ObjectInputStream to read the byte stream in the file as an object.
Sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class DeserializationDemo {
public static void main(String[] args) {
Person person = null;
try {
FileInputStream fileIn = new FileInputStream( "person.ser" );
ObjectInputStream in = new ObjectInputStream(fileIn);
person = (Person) in.readObject();
in.close();
fileIn.close();
} catch (Exception e) {
e.printStackTrace();
}
if (person != null) {
System.out.println( "对象已经从person.ser文件中反序列化:" + person.getName() + "," + person.getAge());
}
}
}
|
Copy after login
The above is the method and code example of using the deserialization function to achieve serialization and deserialization of objects in Java. By implementing the Serializable interface and using the ObjectInputStream and ObjectOutputStream classes, we can easily serialize objects to byte streams, or deserialize byte streams back to objects. This mechanism has great application value in both network transmission and file storage.
The above is the detailed content of How to use the deserialization function to implement serialization and deserialization of objects in Java. For more information, please follow other related articles on the PHP Chinese website!