Java序列化是指把Java對象轉(zhuǎn)換為字節(jié)序列的過程;而Java反序列化是指把字節(jié)序列恢復(fù)為Java對象的過程。
2.為什么需要序列化與反序列化
我們知道,當(dāng)兩個進(jìn)程進(jìn)行遠(yuǎn)程通信時,可以相互發(fā)送各種類型的數(shù)據(jù),包括文本、圖片、音頻、視頻等, 而這些數(shù)據(jù)都會以二進(jìn)制序列的形式在網(wǎng)絡(luò)上傳送。那么當(dāng)兩個Java進(jìn)程進(jìn)行通信時,能否實現(xiàn)進(jìn)程間的對象傳送呢?答案是可以的。如何做到呢?這就需要Java序列化與反序列化了。換句話說,一方面,發(fā)送方需要把這個Java對象轉(zhuǎn)換為字節(jié)序列,然后在網(wǎng)絡(luò)
傳送;另一方面,接收方需要從字節(jié)序列中恢復(fù)出Java對象。
當(dāng)我們明晰了為什么需要Java序列化和反序列化后,我們很自然地會想Java序列化的好處。其好處一是實現(xiàn)了數(shù)據(jù)的持久化,通過序列化可以把數(shù)據(jù)永久地保存到硬盤上(通常存放在文件里),二是,利用序列化實現(xiàn)遠(yuǎn)程通信,即在網(wǎng)絡(luò)上傳送對象的字節(jié)序列。
3.示例:
(1)序列化反序列化文件:
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
|
import java.io.*; @SuppressWarnings ( "serial" ) class Person implements Serializable { public Person(String name, String sex, int age, int height) { this .name = name; this .sex = sex; this .age = age; this .height = height; } public String toString() { return "|" + this .name + "|" + this .sex + "|" + this .age + "|" + this .height + "|" ; } public String name; public String sex; public int age; public int height; } public class SerialTest { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Person p = new Person( "Jim" , "male" , 28 , 194 ); // 開始序列化 ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream( new File( "myTest.txt" ))); oos.writeObject(p); // 反序列化 ObjectInputStream ois = new ObjectInputStream( new FileInputStream( new File( "myTest.txt" ))); Person p1 = (Person) ois.readObject(); System.out.println(p1.toString()); } } |
(2)XML反序列化成class:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
import java.io.*; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; @SuppressWarnings ( "serial" ) class RoadInfo implements Serializable { public int id; public long MDN; public String NAME; public double LNG; public double LAT; public String ICON; } @SuppressWarnings ( "serial" ) class table_list implements Serializable { public String toString() { StringBuffer sb = new StringBuffer(); for (RoadInfo r : sequence) { sb.append( "|" ); sb.append(r.id); sb.append( "|" ); sb.append(r.MDN); sb.append( "|" ); sb.append(r.NAME); sb.append( "|" ); sb.append(r.LNG); sb.append( "|" ); sb.append(r.LAT); sb.append( "|" ); sb.append(r.ICON); sb.append( "|\n" ); } return sb.toString(); } public table_list( int count) { sequence = new RoadInfo[count]; for ( int i = 0 ; i < count; i++) { sequence[i] = new RoadInfo(); } } public RoadInfo[] sequence; } public class XMLTest { /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); BufferedReader reader = new BufferedReader( new FileReader( new File( "friend_msg.xml" ))); while ( true ) { String s = reader.readLine(); // 讀一行 if (s == null ) { break ; } sb.append(s); } XStream xs = new XStream( new DomDriver()); table_list db = (table_list) xs.fromXML(sb.toString()); System.out.println(db.toString()); } } |