JavaIO 系统

1、javaIO系统的设计使用了装饰器模式:

很少使用单个类来创建对象,而是通过叠合对各对象来提供所需要的功能。

(将多个对象层叠在一起,才能得到自己想要的对象)。 

 

inputStream read()和ouputStream 中的write()一字节或字节数组读写数据,不是经常使用的方法,而是用为了其他类的调用,

DataInputStream d=new DataInputStream(new BufferedInputStream(new FileInputStream(“ filePath”)));

 

1)打开文件,以便能输入:使用FileInputStream

2)为了提高速度,需要将文件进行缓存处理:使用BufferedInputStream

3)以格式化的形式读取输入的数据, DataInputStream

 

2、标准输入输出:

System.out   System.err   System.in

System.out /err 都进行了封装,而system.in没有进行封装,在使用时要进行封装。

DataInputStream d=new DataInputStream(new BufferedInputStream(System.in));

 

javaI/O类库中需要

FileterInputStream FileterOutputStream 用来提供装饰类接口,以控制特定输入流和输出流的两个类,是装饰器的必要条件,(以便能为所有正在修饰的对象提供通用接口)

 程序代码参见:

http://www.cnblogs.com/liuling/archive/2013/05/08/bufferedStream.html

http://www.cnblogs.com/liuling/archive/2013/05/07/InputStreamAndOutputStream.html (对输入输出流的讲解)

1、缓冲输入文件

 public String read()throws IOException{
 BufferedReader in=new BufferedReader(new FileReader(""));
 String s="";
 StringBuilder sb=new StringBuilder();
 while((s=in.readLine())!=null){
 sb.append(s+'\n');
 }
 in.close();
 return  sb.toString();
  }

按字节读取:


  int s=-1;
  byte[] b=new byte[1024];
  while((s=bis.read(b))!=-1){
  sb.append(new String(b,0,s));
  fo.write(b);
  }
  bis.close();

 

转载于:https://www.cnblogs.com/yeemi/p/7470187.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值