缓冲流的使用

缓冲流的使用

一,缓冲流的使用

1.缓冲流

BufferedInputStream 字节

BufferedOutputStream

BufferedReader 字符

BufferedWriter

2.作用

提供流的读取,写入的速度

提高读写速度的原因:内部提供了一个缓冲区

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

二,实现非文本文件的复制

固定的代码构建

@Test
    public void bufferedStreamTest() {
        //1.造文件
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File srcFile = new File("1.jpg");
            File destFile = new File("12.jpg");
            //2.造流
            //2.1造节点流
            FileInputStream fis = new FileInputStream(srcFile);
            FileOutputStream 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);

//                bos.flush();//刷新缓冲区
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源流:先关闭外层流,再关闭内层流
            if (bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略.
//        fos.close();
//        fis.close();
    }

实现文件复制的方法

public void copyFileBuffered(String srcPath, String destPath){
    //1.造文件
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        //2.造流
        //2.1造节点流
        FileInputStream fis = new FileInputStream(srcFile);
        FileOutputStream fos = new FileOutputStream(destFile);
        //2.2 造缓冲流
        bis = new BufferedInputStream(fis);
        bos = new BufferedOutputStream(fos);

        //3.复制的细节:读取,写入
        byte[] buffer = new byte[1024];
        int len;
        while ((len = bis.read(buffer))!=-1){
            bos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        //4.关闭资源流:先关闭外层流,再关闭内层流
        if (bos!=null){
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bis!=null){
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@Test
public void testCopyFileBuffered(){
    long start = System.currentTimeMillis();
    String src = "D:\\BaiduNetdiskDownload\\Anaconda3-2021.05-Linux-x86_64.sh";
    String dest = "D:\\BaiduNetdiskDownload\\1.sh";
    copyFileBuffered(src,dest);

    long end = System.currentTimeMillis();
    System.out.println("复制操纵花费的时间为:"+(end-start));// 节点流byte[1024] 2788ms  缓冲流byte[1024]  940ms
}
三,使用BufferedReader和BufferedWriter实现文本文件的复制
@Test
    public void testBufferedReaderBufferedWrite() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //创建文件和相应的流
            br = new BufferedReader(new FileReader(new File("dbcp.txt")));
            bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));

            //读写操作
            //方式一:
//            char[] cbuf = new char[1024];
//            int len;
//            while ((len = br.read(cbuf))!= -1){
//                bw.write(cbuf,0,len);
//    //            bw.flush();
//            }
            //方式二:String
            String data;
            while ((data = br.readLine())!= null){
                //方法一:
//                bw.write(data + "\n");//data中不包含换行符
                //方法二:
                bw.write(data);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
posted @   不落微笑  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示