數(shù)據(jù)操作流
在io包中友浸,提供了兩個(gè)與平臺(tái)無關(guān)的數(shù)據(jù)操作流:
數(shù)據(jù)輸出流(DataOutputStream)
數(shù)據(jù)輸入流 (DataInputStream)
通常數(shù)據(jù)輸出流會(huì)按照一定的格式將數(shù)據(jù)輸出,再通過數(shù)據(jù)輸入流按照一定的格式將數(shù)據(jù)讀入囊拜。
例如:有如下的訂單數(shù)據(jù)
如果要想使用數(shù)據(jù)操作流钢坦,則肯定要由用戶自己制定數(shù)據(jù)的保存格式究孕,必須按指定好的格式保存數(shù)據(jù),才可以使用數(shù)據(jù)輸入流將數(shù)據(jù)讀取進(jìn)來爹凹。
DataOutputStream:DataOutputStream是OutputStream的子類厨诸,此類的定義如下:
public class DataOutputStream extends FilterOutputStream implements DataOutput
此類繼承自FilterOutputStream類(FilterOutputStream是OutputStream的子類)同時(shí)實(shí)現(xiàn)了DataOutput接口,在DataOutput接口中定義了一系列的寫入各種數(shù)據(jù)的方法禾酱。writeXxx()
要想使用DataOutputStream寫入數(shù)據(jù)的話微酬,則必須指定好數(shù)據(jù)的輸出格式绘趋。
數(shù)據(jù)的寫入格式:
以上每條數(shù)據(jù)之間使用"\n"分隔,每條數(shù)據(jù)中的每個(gè)內(nèi)容之間使用"\t"分隔颗管。如下圖所示:
[java] view plain copy
<embed id="ZeroClipboardMovie_1" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_1" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=1&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- 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 ; // 聲明數(shù)據(jù)輸出流對(duì)象
- File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路徑
- dos = new DataOutputStream(new FileOutputStream(f)) ; // 實(shí)例化數(shù)據(jù)輸出流對(duì)象
- String names[] = {"襯衣","手套","圍巾"} ; // 商品名稱
- float prices[] = {98.3f,30.3f,50.5f} ; // 商品價(jià)格
- int nums[] = {3,2,1} ; // 商品數(shù)量
- for(int i=0;i<names.length;i++){ // 循環(huán)輸出
- dos.writeChars(names[i]) ; // 寫入字符串
- dos.writeChar('\t') ; // 寫入分隔符
- dos.writeFloat(prices[i]) ; // 寫入價(jià)格
- dos.writeChar('\t') ; // 寫入分隔符
- dos.writeInt(nums[i]) ; // 寫入數(shù)量
- dos.writeChar('\n') ; // 換行
- }
- dos.close() ; // 關(guān)閉輸出流
- }
- };
使用DataOutputStream寫入的數(shù)據(jù)要使用DataInputStream讀取進(jìn)來陷遮。
[java] view plain copy
<embed id="ZeroClipboardMovie_2" src="https://csdnimg.cn/public/highlighter/ZeroClipboard.swf" loop="false" menu="false" quality="best" bgcolor="#ffffff" name="ZeroClipboardMovie_2" allowscriptaccess="always" allowfullscreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="id=2&width=16&height=16" wmode="transparent" width="16" height="16" align="middle">
- 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 ; // 聲明數(shù)據(jù)輸入流對(duì)象
- File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路徑
- dis = new DataInputStream(new FileInputStream(f)) ; // 實(shí)例化數(shù)據(jù)輸入流對(duì)象
- String name = null ; // 接收名稱
- float price = 0.0f ; // 接收價(jià)格
- int num = 0 ; // 接收數(shù)量
- char temp[] = null ; // 接收商品名稱
- int len = 0 ; // 保存讀取數(shù)據(jù)的個(gè)數(shù)
- char c = 0 ; // '\u0000'
- try{
- while(true){
- temp = new char[200] ; // 開辟空間
- len = 0 ;
- while((c=dis.readChar())!='\t'){ // 接收內(nèi)容
- temp[len] = c ;
- len ++ ; // 讀取長度加1
- }
- name = new String(temp,0,len) ; // 將字符數(shù)組變?yōu)镾tring
- price = dis.readFloat() ; // 讀取價(jià)格
- dis.readChar() ; // 讀取\t
- num = dis.readInt() ; // 讀取int
- dis.readChar() ; // 讀取\n
- System.out.printf("名稱:%s;價(jià)格:%5.2f垦江;數(shù)量:%d\n",name,price,num) ;
- }
- }catch(Exception e){}
- dis.close() ;
- }
- };
5.2f 表示的是 總共的數(shù)字長度為5位帽馋,其中2位表示小數(shù),3位表示整數(shù)比吭。