zoukankan      html  css  js  c++  java
  • 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+' ');
     }
     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();

     

  • 相关阅读:
    如何修补软件、系统漏洞?
    轻松学习Linux之本地安装系统
    看程序体验缓冲区溢出漏洞
    企业网管软件实战之SolarWinds LANsurveyor
    Android项目开发遇到的问题(64K的错误)的解决之路,从入坑到出坑
    史上最佳 Mac+PhpStorm+XAMPP+Xdebug 集成开发和断点调试环境的配置
    [noip2011]计算系数+二项式定理证明
    [nowcoder5668H]Sort the Strings Revision
    (动态规划)导弹防御
    nyoj 79 拦截导弹
  • 原文地址:https://www.cnblogs.com/yeemi/p/7470187.html
Copyright © 2011-2022 走看看