數據操作流
在io包中,提供了兩個與平臺無關的數據操作流:
數據輸入流(datainputstream)
數據輸出流(dataoutputstream)
通常數據輸出流會按一定格式將數據輸出,再通過數據輸入流按照一定格式將數據讀入
dataoutputstream接口定義了一系列的writexxx()的操作,可以寫入各種數據類型的數據。
范例:使用數據操作流寫入與讀出數據
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.io.dataoutputstream ; import java.io.file ; import java.io.fileoutputstream ; public class dataoutputstreamdemo{ public static void main(string args[]) throws exception{ // 所有異常拋出 dataoutputstream dos = null ; // 聲明數據輸出流對象 file f = new file( "d:" + file.separator + "order.txt" ) ; // 文件的保存路徑 dos = new dataoutputstream( new fileoutputstream(f)) ; // 實例化數據輸出流對象 string names[] = { "襯衣" , "手套" , "圍巾" } ; // 商品名稱 float prices[] = { 98 .3f, 30 .3f, 50 .5f} ; // 商品價格 int nums[] = { 3 , 2 , 1 } ; // 商品數量 for ( int i= 0 ;i<names.length;i++){ // 循環輸出 dos.writechars(names[i]) ; // 寫入字符串 dos.writechar( '\t' ) ; // 寫入分隔符 dos.writefloat(prices[i]) ; // 寫入價格 dos.writechar( '\t' ) ; // 寫入分隔符 dos.writeint(nums[i]) ; // 寫入數量 dos.writechar( '\n' ) ; // 換行 } dos.close() ; // 關閉輸出流 } }; |
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
|
import java.io.datainputstream ; import java.io.file ; import java.io.fileinputstream ; public class datainputstreamdemo{ public static void main(string args[]) throws exception{ // 所有異常拋出 datainputstream dis = null ; // 聲明數據輸入流對象 file f = new file( "d:" + file.separator + "order.txt" ) ; // 文件的保存路徑 dis = new datainputstream( new fileinputstream(f)) ; // 實例化數據輸入流對象 string name = null ; // 接收名稱 float price = 0 .0f ; // 接收價格 int num = 0 ; // 接收數量 char temp[] = null ; // 接收商品名稱 int len = 0 ; // 保存讀取數據的個數 char c = 0 ; // '\u0000' try { while ( true ){ temp = new char [ 200 ] ; // 開辟空間 len = 0 ; while ((c=dis.readchar())!= '\t' ){ // 接收內容 temp[len] = c ; len ++ ; // 讀取長度加1 } name = new string(temp, 0 ,len) ; // 將字符數組變為string price = dis.readfloat() ; // 讀取價格 dis.readchar() ; // 讀取\t num = dis.readint() ; // 讀取int dis.readchar() ; // 讀取\n system.out.printf( "名稱:%s;價格:%5.2f;數量:%d\n" ,name,price,num) ; } } catch (exception e){} dis.close() ; } }; |
對象序列化
對象序列化就是把對象變為二進制數據流的一種方法,通過對象的序列化可以方便的實現對象的傳輸或儲存。
如果一個類想支持初始化,則該類必須實現java.io.serilizable接口。該接口定義如下:
publicinterfaceserilizable{}
該接口中不存在方法,因此該類屬于一個標示接口,表示實現該的接口的類具備某種能力。
1.對象的序列化與反序列化
2.serialversionuid
在序列化的操作中引入了一個serialversionuid常量,可以通過此常量來驗證版本的一致性,在進行反序列化的時候,jvm會把傳進來的字節流中的serialversionuid與本地對應類的serialversionuid進行比較,如果相同就認為是一致的,可以進行反序列化,否則就會出現序列化版本不一致的異常。
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.io.serializable ; public class person implementsserializable{ private string name ; // 聲明name屬性,但是此屬性不被序列化 private int age ; // 聲明age屬性 publicperson(string name, int age){ // 通過構造設置內容 this .name= name ; this .age= age ; } publicstring tostring(){ // 覆寫tostring()方法 return "姓名:" + this .name + ";年齡:" + this .age; } }; |
3. 對象輸出流:objectoutputstream
一個對象要想進行輸出,就必須使用objectoutputstream類,該類定義如下
如果一個對象中的某個屬性不希望被序列化的話,則可以使用transient關鍵字進行聲明。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
importjava.io.file ; importjava.io.fileoutputstream ; importjava.io.outputstream ; importjava.io.objectoutputstream ; publicclass serdemo01{ public static void main(string args[]) throwsexception { file f = new file( "d:" +file.separator + "test.txt" ) ; //定義保存路徑 objectoutputstream oos = null ; // 聲明對象輸出流 outputstream out = newfileoutputstream(f) ; // 文件輸出流 oos = new objectoutputstream(out) ; oos.writeobject( new person( "張三" , 30 )); // 保存對象 oos.close() ; // 關閉 } }; |
4. 對象輸入流:objectinputstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
importjava.io.file ; importjava.io.fileinputstream ; importjava.io.inputstream ; importjava.io.objectinputstream ; publicclass serdemo02{ public static void main(string args[]) throwsexception { file f = new file( "d:" +file.separator + "test.txt" ) ; //定義保存路徑 objectinputstream ois = null ; // 聲明對象輸入流 inputstream input = newfileinputstream(f) ; // 文件輸入流 ois = new objectinputstream(input) ; // 實例化對象輸入流 object obj = ois.readobject() ; // 讀取對象 ois.close() ; // 關閉 system.out.println(obj) ; } }; |
6.序列化一組對象
對象輸出時只提供了一個對象的輸出操作(writeobject(objectobj)),并沒有提供多個對象的輸出,如果現在要對多個對象進行序列化的操作,則可以使用對象數組完成,由于數組是引用數據類型,所以可以直接使用object類型進行接收。
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
|
importjava.io.file ; importjava.io.ioexception ; importjava.io.fileoutputstream ; importjava.io.outputstream ; importjava.io.objectoutputstream ; importjava.io.fileinputstream ; importjava.io.inputstream ; importjava.io.objectinputstream ; publicclass serdemo05{ public static void main(string args[]) throwsexception{ person per[] = { new person( "張三" , 30 ),newperson( "李四" , 31 ), new person( "王五" , 32 )}; ser(per) ; object o[] = (object[])dser() ; for ( int i= 0 ;i<o.length;i++){ person p = (person)o[i] ; system.out.println(p) ; } } public static void ser(object obj[]) throwsexception { file f = new file( "d:" +file.separator + "test.txt" ) ; //定義保存路徑 objectoutputstream oos = null ; // 聲明對象輸出流 outputstream out = new fileoutputstream(f); // 文件輸出流 oos = new objectoutputstream(out) ; oos.writeobject(obj) ; // 保存對象 oos.close() ; // 關閉 } public static object[] dser() throws exception{ file f = new file( "d:" +file.separator + "test.txt" ) ; //定義保存路徑 objectinputstream ois = null ; // 聲明對象輸入流 inputstream input = newfileinputstream(f) ; // 文件輸入流 ois = new objectinputstream(input) ; // 實例化對象輸入流 object obj[] =(object[])ois.readobject() ; // 讀取對象 ois.close() ; // 關閉 return obj ; } }; |
數組能儲存的對象數量有限,因此可以使用類集進行序列化的操作。
壓縮流
在java中為了減少傳輸時的數據量也專門提供了壓縮流,可以將文件或者文件夾壓縮成zip、jar、gzip等格式。
該流使用較少,因此只做簡要介紹。
總結
以上就是本文關于java io數據操作流、對象序列化、壓縮流代碼解析的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/Troy__/article/details/25056085