Java IO流详解:重点


Java IO流详解:重点

  • 数据流的基本概念:

    • 把不同类型的输入,输出源抽象为:流(Stream)

    • 其中,输入或输出的数据称为数据流(Data Stream),用统一的接口来表示;

 

  • IO流的分类:

    • 数据流是指一组有顺序的,有起点和终点的字节集合

    • 按照流的流向,可以分为输入流和输出流;,其中输入,输出是针对程序来说的;

       

    • 按照处理单位的不同,分为:字节流和字符流:

      • 字节流:每次读取一个字节,当传输的文件中中文时,就会出现乱码;

      • 字符流:每次读取2个字节,有中文时,使用该流就可以正确传输显示中文;

        (1字符=2个字节,1字节=8位,一个汉字占2个字节)

         

    • 按照角色划分:分为:节点流和处理流:

      • 节点流:从或向一个特定的地方(节点)读写数据;如FileInputStream;

        (直接操作数据的:都是节点流,构造方法都是数据对象)

      • 处理流(包装流):

        • 是对一个已经存在的流的连接和封装;

        • 通过所封装的流的功能调用实现数据读写;

        • 处理流的构造方法总是要带一个其他的流对象做参数;

           

    • Java IO流有4个抽象基类:其他流都是继承这4大基类;

      • 字节流, 字符流

      • 输入流:字节输入流,字符输入流

      • 输出流:字节输出流,字符输出流

         

  • 如何选择适合自己的流:

    • 首先要知道是选择输入流还是输出流;

    • 然后考虑你传输数据时,是每次传一个字节就选字节流;如果存在中文,肯定选字符流;

    • 根据前面2步,就可以选出一个合适的节点流;如果需要在此基础上增强,那么就在处理流中挑选一个即可;

 

  • 字节输入流:inputStream

    • InputStream抽象类:常用方法

      //inputstream类中常用方法:
      ?
      //1, int read():从输入流中读取一个字节的二进制数据;
      //2,int read(byte[] b):将多个字节读取到数组中,填满整个数组;
      //3,int read(byte[] int off, int len):从输入流读取长度位len的数据,从数组b中下标为off的位置开始放置读入的数据,读完返回读取的字节数;
      //4,void close():关闭流数据;
      ?
      ?

       

    • 字节输入流有很多子类:常用的有:

    • ByteArrayInputStream:字节数组输入流

      • ,该类的功能就是从字节数组byte[] 中进行以字节为单位的读取;

      • 也就是将资源文件都以字节形式存入到该类中的字节数组中去

      • 我们拿数据也是从这个字节数组中拿;

    • PipedInputStream:管道字节输入流

      • 它和PipedOutputStream一起使用,能实现多线程间的管道通信;

    • FilterInputStream:装饰着模式中充当装饰者的角色

      • 具体的装饰者都要继承它,所以在该类的子类下都是用来装饰别的流的,也就是处理类;

    • BufferedInputStream:缓冲流

      • 对处理流进行装饰,增强,内部会有一个缓冲区,用来存放字节,每次都素将缓冲区存满后发送,而不是一个字节或2个字节这样发送,效率更高;

    • DataInputSteam:数据输入流:

      • 用来装饰其他输入流,它允许通过数据流来读写Java基本类型;

    • FileInputStream:文件输入流:

      • 通常用来对文件进行读取操作;

    • File:对指定目录的文件进行操作:

      • 对指定目录的文件进行操作:增,删,改,查!

    • ObjectInputStream:对象输入流:

      • 用来提供对基本数据或对象的持久存储;

      • 序列化,反序列化;

       

----------节点流------------------------------------------------------------------------------------------------------------------------------------

 

  • 代码案例:ByteArrayInputStream:read()方法;

    /**
    * 案例1:ByteArrayInputStream:把byte数组中的数据,读取到控制台
    */
    public class Demo02 {
       public static void main(String[] args) {
    ?
           //1,给byte数组赋值
           String s="hello";
           byte[] bytes=s.getBytes();
           
          int d=0;
           //2,声明一个byte数组输入流
           ByteArrayInputStream bis=new ByteArrayInputStream(bytes);
           try {
               //3,读取流中的数据
               while ((d=bis.read())!=-1){//-1:代表流就读取完毕
                   System.out.print((char)d);//强转
              }
    //4,关闭流
               bis.close();
          } catch (Exception e) {
               e.printStackTrace();
          }
    ?
      }
    }
    ?
    ?
  • 代码案例2:toByteArray():方法

    ?
    public class Demo04 {
       public static void main(String[] args) {
    ?
           //1,声明一个字符串,并给byte数组赋值
           String str1="hello";
           byte[] bytes =str1.getBytes();
           //2,创建一个新字节数组,用于接收数据
           byte[] bytes1 =new byte[5];
           ByteArrayOutputStream bos=new ByteArrayOutputStream();
    ?
           try {
               bos.write(bytes);
               bytes1 =bos.toByteArray();//接收数据
    ?
               //3,把byte转换称字符串:因为String类提供byte类型的参数构造方法
               String s=new String(bytes1);
               System.out.println("--:"+s);
    ?
               //4,关闭流:
               bos.close();
          } catch (IOException e) {
               e.printStackTrace();
          }
    ?
      }
    }
    ?
  • 代码案例:FileInputStream: read();

    ?
    /**
    * FileInputStream类
    */
    public class Demo08 {
       public static void main(String[] args) {
    ?
           //1,定义一个文件
           String s="hello";
           File file=new File("E:/haha/demo1.txt");
           try {
               //2,声明一个文件输入流,构造方法总送文件
               FileInputStream fis=new FileInputStream(file);
               int d =0;
               while ((d=fis.read()) != -1){
                   System.out.print((char)d );//123
              }
               //3,关闭流          
               fis.close();
          } catch (Exception e) {
               e.printStackTrace();
          }
    ?
      }
    }
    ?
  • 代码案例:FileOutputStream:write()方法:参数都是byte[];

    ?
    /**
    * FileOutputStream类:
    * 操作的都是byte[] 数据;
    */
    public class Demo09 {
       public static void main(String[] args) {
    ?
           //1,写入数据:可以写入byte[]
           String ss="world";
           byte[] bytes=ss.getBytes();
           //2,定位文件位置
           File file=new File("E:/haha/demo1.txt");
    ?
           try {
               FileOutputStream fos=new FileOutputStream(file);
               fos.write(bytes);//写入byte数据
    ?
               System.out.println("over");
               //3,关闭流
               fos.close();
    ?
          } catch (Exception e) {
               e.printStackTrace();
          }
    ?
    ?
      }
    }
    ?
    ?
    ?

     

----------处理流------------------------------------------------------------------------------------------------------------------------------------

 

  • 代码案例:BufferedInputStream 类: read()方法

    ?
    public class Demo11 {
       public static void main(String[] args) {
    ?
           File file= new File("E:/haha/demo1.txt");
    ?
           try {
               FileInputStream fis=new FileInputStream(file);
               //声明一个缓冲流
               BufferedInputStream bis=new BufferedInputStream(fis);
    ?
               //读取缓冲流
               int d=-1;
               while((d=bis.read())!= -1){
                   System.out.print((char) d);//world
              }
    ?
               //关闭流
               fis.close();
               bis.close();
    ?
    ?
          } catch (IOException e) {
               e.printStackTrace();
          }
    ?
    ?
      }
    ?
    }
  • 代码案例:

    public class Demo12 {
       public static void main(String[] args) {
    ?
           String ss="hello";
           byte[] bytes=ss.getBytes();
    ?
           //声明一个字节流
           ByteArrayInputStream bais=new ByteArrayInputStream(bytes);
           //声明一个处理流
           BufferedInputStream bis=new BufferedInputStream(bais);
    ?
           //读取流数据
           try {
               int d =0;
               while ((d=bis.read()) != -1){
                   System.out.print((char) d);//hello
              }
    ?
               //关闭流
               bais.close();
               bis.close();
    ?
          } catch (IOException e) {
               e.printStackTrace();
          }
    ?
      }
    }
    ?
  • 代码案例:BufferedOutputStream:write();

    ?
    public class Demo14 {
       public static void main(String[] args) {
    ?
           String ss="doubi4";
           byte[] bytes=ss.getBytes() ;
           try {
               FileOutputStream fos=new FileOutputStream("E:/haha/demo1.txt");
    ?
               BufferedOutputStream bos=new BufferedOutputStream(fos);
    ?
               bos.write(bytes);
               System.out.println("over");
    ?
    ?
               fos.close();
               bos.close();
    ?
    ?
          } catch (IOException e) {
               e.printStackTrace();
          }
    ?
    ?
      }
    }