JavaIO 系统

来源:互联网 发布:手机看片下什么软件 编辑:程序博客网 时间:2024/05/17 07:55

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();

 

0 0
原创粉丝点击