IO流(5)—缓冲流

1.IO体系:

  • 抽象基类——节点流(文件流)——缓冲流(处理流的一种)
  • InputStream ——FileInputStream——BufferedInputStream
  • OutputStream——FileOutputSteam——BufferedOutputStream(flush())
  • Reader——FileReader——BufferedReder(readLine())
  • Writer——FileWriter——BufferedWriter(flush())

2.分类
处理流之一:

  • 缓冲流:BufferedInputStream和BufferedOutputStream(字节流)
  • 用于处用于处理视频文件、音频文件、图片、.doc等非文本文件,作用:加速文件传输

  • 缓冲流:BufferedReader和BufferedWriter(字符流)

  • 主要用于处理纯文本文件(.txt)。

3.代码实例

字节缓冲流


public class BufferedInputOutputStream {
    //计算使用缓冲流BufferedInputStream和BufferedOutputStream传输非文本文件的时间
    @Test
    public void spendTime(){
        long start = System.currentTimeMillis();
        String src = "file/1.mp4";
        String dest = "file/2.mp4";
        testBufferedInputOutputStream(src,dest);
        long end = System.currentTimeMillis();
        System.out.println("花费时间为:"+(end - start));//时间为504毫秒
    }
    //使用缓冲流把非文本文件复制一份
    //@Test
    public void testBufferedInputOutputStream(String src,String dest){
        //3.创建缓冲流对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.File类对象
            File file1 = new File(src);
            File file2 = new File(dest);
            //2.创建节点流对象
            FileInputStream fis = new FileInputStream(file1);
            FileOutputStream fos = new FileOutputStream(file2);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] b = new byte[1024];
            int len;
            //4.开始读取文件,写入文件
            while((len = bis.read(b)) != -1){
                bos.write(b,0,len);
                //flush()是强行将缓冲区中的内容输出,否则直到缓冲区满后才会一次性的将内容输出
                bos.flush();//刷新
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(bos != null){
                //5.关闭流
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

字符缓冲流

public class BufferedReaderWriter {
    //把文件文件复制到另一个文本文件
    @Test
    public void testBufferedReaderWriter(){
        //3.定义两个缓冲流
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //1.定义两个文件对象
            File file = new File("file/hello.txt");
            File file1 = new File("file/hello5.txt");
            //2.定义两个节点流
            FileReader fr = new FileReader(file);
            FileWriter fw = new FileWriter(file1);
            br = new BufferedReader(fr);
            bw = new BufferedWriter(fw);
            //4.开始读取文件——方法一
            /*char[] c = new char[1024];
            int len;
            while((len = br.read(c)) != -1){
                bw.write(c, 0, len);
            }*/
            //开始读取文件——方法二
            String str;
            while((str = br.readLine()) != null){
                bw.write(str+"
");//或者bw.newLine();
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //5.关闭
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
原文地址:https://www.cnblogs.com/tengpengfei/p/10454004.html