本文實例講述了java對象序列化操作。分享給大家供大家參考,具體如下:
當兩個進程在進行遠程通信時,彼此可以發送各種類型的數據。無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。發送方需要把這個java對象轉換為字節序列,才能在網絡上傳送;接收方則需要把字節序列再恢復為java對象。
只能將支持 java.io.serializable 接口的對象寫入流中。每個 serializable 對象的類都被編碼,編碼內容包括類名和類簽名、對象的字段值和數組值,以及從初始對象中引用的其他所有對象的閉包。
概念
序列化:把java對象轉換為字節序列的過程。
反序列化:把字節序列恢復為java對象的過程。
用途
對象的序列化主要有兩種用途:
1) 把對象的字節序列永久地保存到硬盤上,通常存放在一個文件中;
2) 在網絡上傳送對象的字節序列。
對象序列化
序列化api
java.io.objectoutputstream
代表對象輸出流,它的writeobject(object obj)
方法可對參數指定的obj對象進行序列化,把得到的字節序列寫到一個目標輸出流中。只有實現了serializable和externalizable接口的類的對象才能被序列化。
java.io.objectinputstream
代表對象輸入流,它的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
27
28
29
30
31
32
|
import java.io.*; import java.util.date; public class objectsaver { public static void main(string[] args) throws exception { /*其中的 d:\\objectfile.obj 表示存放序列化對象的文件*/ //序列化對象 objectoutputstream out = new objectoutputstream( new fileoutputstream( "d:\\objectfile.obj" )); customer customer = new customer( "王麻子" , 24 ); out.writeobject( "你好!" ); //寫入字面值常量 out.writeobject( new date()); //寫入匿名date對象 out.writeobject(customer); //寫入customer對象 out.close(); //反序列化對象 objectinputstream in = new objectinputstream( new fileinputstream( "d:\\objectfile.obj" )); system.out.println( "obj1 " + (string) in.readobject()); //讀取字面值常量 system.out.println( "obj2 " + (date) in.readobject()); //讀取匿名date對象 customer obj3 = (customer) in.readobject(); //讀取customer對象 system.out.println( "obj3 " + obj3); in.close(); } } class customer implements serializable { private string name; private int age; public customer(string name, int age) { this .name = name; this .age = age; } public string tostring() { return "name=" + name + ", age=" + age; } } |
執行結果
說明
讀取對象的順序與寫入時的順序要一致。
對象的默認序列化機制寫入的內容是:對象的類,類簽名,以及非瞬態和非靜態字段的值。
常見序列化操作
打印流
1
2
3
4
5
6
7
8
9
10
11
|
public class hello { public static void main(string[] args) throws exception { file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" ); outputstream outputstream = new fileoutputstream(file); printstream printstream = new printstream(outputstream); printstream.print( 123 ); printstream.println( "hello" ); printstream.println( 12.5 ); printstream.close(); } } |
鍵盤輸入讀取到程序中
1
2
3
4
5
6
7
8
9
10
|
public class hello { public static void main(string[] args) throws exception { inputstream in = system.in; byte [] data = new byte [ 100 ]; system.out.println( "輸入數據:" ); int read = in.read(data); system.out.println(read); system.out.println( new string(data, 0 ,read)); } } |
掃碼流
1
2
3
4
5
6
7
8
9
10
11
|
public class hello { public static void main(string[] args) throws exception { scanner scanner = new scanner( new fileinputstream( new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "123.txt" ))); scanner.usedelimiter( "\n" ); while (scanner.hasnext()){ string next = scanner.next(); system.out.println(next); } scanner.close(); } } |
scanner.usedelimiter("\n");
表示以回車(換行)為定界符,回車間為一段掃碼的內容。
掃描鍵盤輸入
1
|
scanner scanner = new scanner(system.in); |
注意:使用while判斷鍵盤輸入,程序可能會無法結束
對象序列化
序列化操作類:objectoutputstream,寫到文件中
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
|
public class hello { public static void main(string[] args) throws exception { a a = new a( "hello" , 123 ); file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "a.ser" ); outputstream outputstream = new fileoutputstream(file); objectoutputstream objectoutputstream = new objectoutputstream(outputstream); objectoutputstream.writeobject(a); objectoutputstream.close(); } } class a implements serializable { private string title; private integer number; public a(string title, integer number) { this .title = title; this .number = number; } public string gettitle() { return title; } public void settitle(string title) { this .title = title; } public integer getnumber() { return number; } public void setnumber(integer number) { this .number = number; } @override public string tostring() { return "a{" + "title='" + title + '\ '' + ", number=" + number + '}' ; } } |
實體需要實現可序列化的接口implements serializable
,表示一種能力
反序列化操作類:objectinputstream,讀到程序里
1
2
3
4
5
6
7
8
9
|
public class hello { public static void main(string[] args) throws exception { file file = new file( "e:" + file.separator + "myfile" + file.separator + "test" + file.separator + "a.ser" ); inputstream inputstream = new fileinputstream(file); objectinputstream objectinputstream = new objectinputstream(inputstream); a a = (a) objectinputstream.readobject(); system.out.println(a); } } |
transient
關鍵字,實體的屬性使用該關鍵子,進行序列化時該屬性值將不會被保存,反序列化的結果為,該屬性的值為該屬性類型的默認值。
1
2
|
private string title; private transient integer number; |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:https://blog.csdn.net/shuair/article/details/82013196