In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.