缓冲流

缓冲流

缓冲流的使用

ogLVY.png

缓冲流是处理流的一种

处理流就是“套接”在已有的流的基础上

分类

字节:

  • BufferedInputStream
  • BufferedOutputStream

字符:

  • BufferedReader
  • BufferedWriter

作用

缓冲流的主要作用是提高文件的读写效率

BufferedInputStream和BufferedOutputStream的使用

字节型

过程

  1. 实例化File类的对象,指明要操作的文件

  2. 提供具体的流

    2.1 造节点流

    2.2 造缓冲流

  3. 数据的写出输入过程

  4. 流的关闭操作

    • 关闭顺序的要求:先关闭外层的流,再关闭内层的流
  5. try-catch-finally异常处理

说明

  1. 在提供具体的流的时候,先造内层的FileInputStream(FileOutputStream)流,再造外层BufferedInputStream(BufferedOutputStream)流
  2. 在关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭可以省略

代码实现

FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
    //1. 实例化File类的对象,指明要操作的文件
    File srcFile = new File("C:\\Users\\LENOVO\\Desktop\\丸子.png");
    File destFile = new File("C:\\Users\\LENOVO\\Desktop\\殷志源.png");
    //2. 提供具体的流
    //2.1 造节点流
    fis = new FileInputStream(srcFile);
    fos = new FileOutputStream(destFile);
    //2.2 造缓冲流
    bis = new BufferedInputStream(fis);
    bos = new BufferedOutputStream(fos);
    //3. 读取和写入过程
    byte[] buffer = new byte[10];
    int len;
    while((len = bis.read(buffer)) != -1 ){
        bos.write(buffer,0,len);
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    //4. 关闭
    try {
        if(bis != null)
            bis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        if(bos != null)
            bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    //关闭内层流省略
//   try {
//        if(fos != null)
//            fos.close();
//        } catch (IOException e) {
//           e.printStackTrace();
//        }
//
//   try {
//        if(fis != null)
//           fis.close();
//        } catch (IOException e) {
//          e.printStackTrace();
//        }
}

运行结果

og8nI.png

缓冲流与节点流读写速度对比

两次代码的byte[]数组设定的长度相同,都是byte[1024],复制的都是同一个视频

利用节点流花费的时间为339(在节点流章节2.2中可以查看)
https://www.cnblogs.com/CrabDumplings/p/13453353.html

利用缓冲流花费的时间为77

ogbdG.png

缓冲流提高读写速度的原因

其内部提供了一个缓冲区

文件在读取的时候先将文件读取到缓冲区,达到缓冲区指定大小后一次性的写出


BufferedOutputStream中有一个方法

bos.flush; //刷新缓冲区(会自动刷新,不用显式调用)

超出缓冲区指定的大小的时候,会自动的刷新出去(把缓冲区的数据情况,将数据写出去)

BufferedReader和BufferedWriter的使用

和BufferedInputStream和BufferedOutputStream用法相似

  • BufferedInputStream和BufferedOutputStream“包”的是FileInputStream和FileOutputStream

  • BufferedReader和BufferedWriter“包”的是FileReader和FileWriter

具体原因见下图( https://www.cnblogs.com/CrabDumplings/p/13441782.html

ogyr1.png

说明

读操作的步骤中的两种写法

  • 和BufferedInputStream一样
    char[] cbuf = new char[1024];
    int len;
    while((len = bfr.read(cbuf)) != -1){
        bfw.write(cbuf,0,len);
    }
    
  • 利用BufferedReader中的readLine()方法和String
    String data;
    while((data = bfr.readLine()) != null){
        bfw.write(data + "\n");
    }
    
    • readLine():一行一行读取数据,但是读取的data中不包含换行符,如果只是如下这样写,最后复制的结果所有文字都在一行

      bfw.write(data );

      • 添加换行的方法一:
        加上换行符(如上面列举的例子)
      • 添加换行的方法二:newLine()方法(BufferedWriter中的方法)
      bfw.write(data);
      bfw.newLine();
      

代码实现

文本文件的复制

public void copytxt(String srcPath, String destPath){
    BufferedReader bfr = null;
    BufferedWriter bfw = null;
    try {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        bfr = new BufferedReader(new FileReader(srcFile));
        bfw = new BufferedWriter(new FileWriter(destFile));

        //方式一
        // char[] cbuf = new char[1024];
        // int len;
        // while((len = bfr.read(cbuf)) != -1){
        //     bfw.write(cbuf,0,len);
        // }

        //方式二
        String data;
        while((data = bfr.readLine()) != null){
            //2.1
//       bfw.write(data + "\n");//data中不包含换行符
            //2.2
            bfw.write(data);
            bfw.newLine();
        }

        System.out.println("复制成功");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if(bfr != null)
                bfr.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            if(bfw != null)
                bfw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Test
public void test6(){
    String srcPath = "C:\\Users\\LENOVO\\Desktop\\hello.txt";
    String destPath = "C:\\Users\\LENOVO\\Desktop\\hello(1).txt";

    long start = System.currentTimeMillis();

    copytxt(srcPath,destPath);

    long end = System.currentTimeMillis();

    System.out.println("花费的时间:" + (end - start));
}
posted @ 2020-08-08 16:46  南煎丸子  阅读(260)  评论(0编辑  收藏  举报