In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to use object flow and serialization in Java". 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 use object flow and serialization in Java.
It looks pretty simple, so take a look at the following example:
Public class Student {
Private int id
Private String name
Private String sex
Private String tel
/ / getter,setter is omitted here
Public Student (int id, String name, String sex, String tel) {
Super ()
This.id = id
This.name = name
This.sex = sex
This.tel = tel
}
}
Public class TestObjectStream {
Public static void main (String [] args) {
WriteObject ()
ReadObject ()
}
Public static void writeObject () {
Try {
FileOutputStream fos = newFileOutputStream ("C:/Users/wangliang/Desktop/1")
ObjectOutputStream oos = new ObjectOutputStream (fos)
Student student1 = new Student (1, "Mary", "female", "11111")
Student student2 = new Student (2, "Jack", "male", "2222")
Student student3 = new Student (3, "Andy", "male", "3333")
List list = Arrays.asList (student1, student2, student3)
Oos.writeObject (list); / / write object
Oos.flush ()
Fos.close ()
Oos.close ()
} catch (Exception e) {
E.printStackTrace ()
}
}
Public static void readObject () {
Try {
FileInputStream fis = new FileInputStream ("C:/Users/wangliang/Desktop/1")
ObjectInputStream ois = new ObjectInputStream (fis)
List list = (List) ois.readObject (); / / read the object
List.forEach (System.out::println)
Fis.close ()
Ois.close ()
} catch (Exception e) {
E.printStackTrace ()
}
}
}
Then the following exception is found during the running process:
Java.io.NotSerializableException: com.qianfeng.day33.entity.Student
At java.io.ObjectOutputStream.writeObject0 (ObjectOutputStream.java:1184)
At java.io.ObjectOutputStream.writeArray (ObjectOutputStream.java:1378)
At java.io.ObjectOutputStream.writeObject0 (ObjectOutputStream.java:1174)
At java.io.ObjectOutputStream.defaultWriteFields (ObjectOutputStream.java:1548)
At java.io.ObjectOutputStream.writeSerialData (ObjectOutputStream.java:1509)
At java.io.ObjectOutputStream.writeOrdinaryObject (ObjectOutputStream.java:1432)
At java.io.ObjectOutputStream.writeObject0 (ObjectOutputStream.java:1178)
At java.io.ObjectOutputStream.writeObject (ObjectOutputStream.java:348)
At com.qianfeng.day33.test.TestObjectStream.writeObject (TestObjectStream.java:28)
At com.qianfeng.day33.test.TestObjectStream.main (TestObjectStream.java:16)
According to the description of this exception, the entity class is not serialized, plus the serialized code to try, as follows:
Public class Student implements java.io.Serializable {
Private static final long serialVersionUID = 4500674287188895027L
Private int id
Private String name
Private String sex
Private String tel
/ / getter,setter is omitted here
Public Student (int id, String name, String sex, String tel) {
Super ()
This.id = id
This.name = name
This.sex = sex
This.tel = tel
}
}
Just implement a java.io.Serializable interface, but take a look at the description of the interface source code:
*
* @ author unascribed
* @ see java.io.ObjectOutputStream
* @ see java.io.ObjectInputStream
* @ see java.io.ObjectOutput
* @ see java.io.ObjectInput
* @ see java.io.Externalizable
* @ since JDK1.1
, /
Public interface Serializable {
}
It is found that there is nothing in the interface. @ see in the above description is almost followed by some operations on the object stream, indicating that the role of serialization is related to the object flow, so what does serialization do?
We can look at the serialization requirements in other languages by analogy, such as the requirements in OC as follows:
/ / data of the stream (JSON data)-> OC object
+ (id) JSONObjectWithData: (NSData *) data options: (NSJSONReadingOptions) opt error: (NSError * *) error
/ / OC object-> data of the stream (Json data)
+ (NSData *) dataWithJSONObject: (id) obj options: (NSJSONWritingOptions) opt error: (NSError * *) error
In OC, if an entity class needs to be serialized, you must implement these two methods. The purpose of these two methods is to tell the system how to convert the entity class to a JSON data (specify key and value) when you need to stream or save the entity class to a file, and then convert it to binary (byte array) using strings.
So why not need it in Java? Because there is a reflection mechanism in Java, this can be done easily.
So what is the role of serial numbers in serialization? Is to ensure that serialization and deserialization can be done correctly, and the conversion can be successful only if the sequence numbers are the same.
At this point, I believe you have a deeper understanding of "object flow and serialization in Java". 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.