How to serialize / deserialize class objects
This article focuses on "how to serialize / deserialize class objects". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to serialize / deserialize class objects.
Serialization and deserialization processing
With serialization supporting classes, you can use the following two classes if you want to implement serialization and deserialization operations.
Serialization: ObjectOutputStream:
Class definition: public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants Construction method: public ObjectOutputStream (OutputStream out) throws IOException Operation method: public final void writeObject (Object obj) throws IOException
Deserialization: ObjectInputStream:
Class definition: public class ObjectInputStream extends InputStream implements ObjectInput,ObjectStreamConstants Construction method: public ObjectInputStream (InputStream in) throws IOException Operation method: public final Object readObject () throws IOException, ClassNotFoundException
Example: implementing serialization and deserialization
Import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;@SuppressWarnings ("serial") class Person implements Serializable {/ / Person class can be serialized private String name;private int age;public Person (String name, int age) {this.name = name;this.age = age;} @ Overridepublic String toString () {return "name:" + this.name + ", age:" + this.age " }} public class JavaAPIDemo {private static final File SAVE_FILE=new File ("D:" + File.separator + "mldn.person"); public static void main (String [] args) throws Exception {/ / saveObject (new Person ("Little sneeze", 78)); System.out.println (loadObject ());} public static void saveObject (Object obj) throws Exception {ObjectOutputStream oos=new ObjectOutputStream (new FileOutputStream (SAVE_FILE)); oos.writeObject (obj); / / serialize oos.close () } public static Object loadObject () throws Exception {ObjectInputStream ois=new ObjectInputStream (new FileInputStream (SAVE_FILE)); Object obj= ois.readObject (); / / deserialize ois.close (); return obj;}}
Object serialization and deserialization in Java must use the internally provided object operation stream, which involves the format of binary data, so it cannot be customized. In addition, if you want to serialize a set of objects, you can use an object array.
In many actual project development processes, developers rarely see direct manipulation of ObjectOutputStream and ObjectInputStream classes, because there are some containers that help developers implement them automatically.
At this point, I believe you have a deeper understanding of "how to serialize / deserialize class objects". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!