【JAVA IO】_数据操作流笔记

【JAVA IO】_数据操作流笔记

分类: Java
【JAVA IO】_数据操作流笔记

本章目标:
掌握DataOutputStream和DataInputStream的作用
可以使用DataOutputStream和DataInputStream写入和读取数据

数据操作流

在io包中,提供了两个与平台无关的数据操作流:
数据输出流(DataOutputStream)
数据输入流(DataInputStream)
通常数据输出流会按照一定的格式将数据输出,再通过数据输入流按照一定的格式将数据读入。

DataOutputStream是OutputStream的子类,此类定义如下:
public class DataOutputStream extends FilterOutputStream implements DataOutput
DataOutput接口定义了一系列的writeXxx()的操作,可以写入各种数据类型的数据。
此接口的定义格式应该大致了解,便于以后学习。

要想使用DataOutputStream写入数据的话,则必须指定好数据的输出格式。

[java]  view plain copy print ?
  1. import java.io.DataOutputStream ;  
  2. import java.io.File ;  
  3. import java.io.FileOutputStream ;  
  4. public class DataOutputStreamDemo{  
  5.     public static void main(String args[]) throws Exception{    // 所有异常抛出  
  6.         DataOutputStream dos = null ;            // 声明数据输出流对象  
  7.         File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路径  
  8.         dos = new DataOutputStream(new FileOutputStream(f)) ;    // 实例化数据输出流对象  
  9.         String names[] = {"衬衣","手套","围巾"} ;    // 商品名称  
  10.         float prices[] = {98.3f,30.3f,50.5f} ;        // 商品价格  
  11.         int nums[] = {3,2,1} ;    // 商品数量  
  12.         for(int i=0;i<names.length;i++){    // 循环输出  
  13.             dos.writeChars(names[i]) ;    // 写入字符串  
  14.             dos.writeChar('\t') ;    // 写入分隔符  
  15.             dos.writeFloat(prices[i]) ; // 写入价格  
  16.             dos.writeChar('\t') ;    // 写入分隔符  
  17.             dos.writeInt(nums[i]) ; // 写入数量  
  18.             dos.writeChar('\n') ;    // 换行  
  19.         }  
  20.         dos.close() ;    // 关闭输出流  
  21.     }  
  22. };  



[java]  view plain copy print ?
  1. import java.io.DataInputStream ;  
  2. import java.io.File ;  
  3. import java.io.FileInputStream ;  
  4. public class DataInputStreamDemo{  
  5.     public static void main(String args[]) throws Exception{    // 所有异常抛出  
  6.         DataInputStream dis = null ;        // 声明数据输入流对象  
  7.         File f = new File("d:" + File.separator + "order.txt") ; // 文件的保存路径  
  8.         dis = new DataInputStream(new FileInputStream(f)) ;    // 实例化数据输入流对象  
  9.         String name = null ;    // 接收名称  
  10.         float price = 0.0f ;    // 接收价格  
  11.         int num = 0 ;    // 接收数量  
  12.         char temp[] = null ;    // 接收商品名称  
  13.         int len = 0 ;    // 保存读取数据的个数  
  14.         char c = 0 ;    // '\u0000'  
  15.         try{  
  16.             while(true){  
  17.                 temp = new char[200] ;    // 开辟空间  
  18.                 len = 0 ;  
  19.                 while((c=dis.readChar())!='\t'){    // 接收内容  
  20.                     temp[len] = c ;  
  21.                     len ++ ;    // 读取长度加1  
  22.                 }  
  23.                 name = new String(temp,0,len) ;    // 将字符数组变为String  
  24.                 price = dis.readFloat() ;    // 读取价格  
  25.                 dis.readChar() ;    // 读取\t  
  26.                 num = dis.readInt() ;    // 读取int  
  27.                 dis.readChar() ;    // 读取\n  
  28.                 System.out.printf("名称:%s;价格:%5.2f;数量:%d\n",name,price,num) ;  
  29.             }  
  30.         }catch(Exception e){}  
  31.         dis.close() ;  
  32.     }  
  33. };  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值