一、什么是?為什么需要?
序列化(serialization)是將對象的狀態信息轉化為可以存儲或者傳輸的形式的過程,反序列化則為其逆過程。
內存的易失性;傳輸需要;一些應用場景中需要將對象持久化下來,以便在需要的時候進行讀取。
二、jdk提供的api
java.io.objectoutputstream類的 writeobject(object obj)方法
java.io.objectinputstream類的readobject()方法
對于serializable,如果沒有重寫 writeobject和readobject,則調用默認的方法
externalizable繼承了serializable,多了2個方法:writeexternal和readexternal,用來控制需要序列化哪些字段
三、實現方法
假定一個person類,實現了serializable或externalizable接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
import java.io.serializable; /** * @author: pf_xu * @date: 2019/3/5 12:37 * @version 1.0 */ public class person implements serializable { private int age; private string name; public person( int age, string name) { this .age = age; this .name = name; } public void setage( int age) { this .age = age; } public void setname(string name) { this .name = name; } public int getage() { return age; } public string getname() { return name; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import java.io.externalizable; import java.io.ioexception; import java.io.objectinput; import java.io.objectoutput; /** * @author: pf_xu * @date: 2019/3/5 13:01 * @version 1.0 */ public class specialperson implements externalizable { private int age; private string name; public specialperson(){} public specialperson( int age, string name) { this .age = age; this .name = name; } public void setage( int age) { this .age = age; } public void setname(string name) { this .name = name; } public int getage() { return age; } public string getname() { return name; } @override public void writeexternal(objectoutput out) throws ioexception { out.writeobject(age); out.writeobject(name); } @override public void readexternal(objectinput in) throws ioexception, classnotfoundexception { this .age = (integer) in.readobject(); this .name = (string)in.readobject(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import java.io.*; /** * @author: pf_xu * @date: 2019/3/5 12:40 * @version 1.0 */ public class serializabledemo { public static void main(string[] args) throws ioexception, classnotfoundexception { person person = new person( 10 , "simon" ); objectoutputstream oos1 = new objectoutputstream( new fileoutputstream( "object1.out" )); oos1.writeobject(person); objectinputstream ois1= new objectinputstream( new fileinputstream( "object1.out" )); person re_person = (person) ois1.readobject(); system.out.println(re_person.getname()+ "---" +re_person.getage()); specialperson specialperson = new specialperson( 30 , "daniel" ); objectoutputstream oos2 = new objectoutputstream( new fileoutputstream( "object2.out" )); oos2.writeobject(specialperson); objectinputstream ois2= new objectinputstream( new fileinputstream( "object2.out" )); specialperson re_specialperson = (specialperson)ois2.readobject(); system.out.println(re_specialperson.getname()+ "---" +re_specialperson.getage()); } } |
四、一些細節
1.序列化id
serialversionuid 如果兩個類的id不同,則不能互相序列與反序列(可應用與版本控制,不同版本的類相互兼容或者不兼容)
2.安全性
由于其標準化導致其有泄露的風險(二進制明文,可采用加密的方法)
以上所述是小編給大家介紹的java序列化和反序列化詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/xupengfei/p/10476368.html