JavaIO流——缓冲流

缓冲流的作用及如何使用

作用

  • 提高文件写入与读取的速度
  • 原因:内部定义了一个缓冲区

使用

  1. 造文件
  2. 造流
    1. 造节点流
    2. 造缓冲流
  3. 处理文件
  4. 关闭流:先关闭处理流再关闭节点流。系统默认关闭了处理流会自动关闭节点流

使用BufferedInputStream和BufferedOutputStream

//使用缓冲流实现非文本文件的传输
    @Test
    public void test01(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //1.造文件
            File srcFile = new File("img.png");
            File destFile = new File("img2.png");
            //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[] bytes = new byte[10];
            int len;
            while ((len = bis.read(bytes)) != -1){
                bos.write(bytes,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();
            }
        }
    }

使用BufferedReader和BufferedWriter

@Test
    public void test01(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("helloworld.txt")));
            bw = new BufferedWriter(new FileWriter(new File("hello2.txt")));

            //方式一:
//            char[] chars = new char[1024];
//            int len;
//            while ((len = br.read(chars)) != -1){
//                bw.write(chars,0,len);
//            }

            //方式二:
            String strFile;
            while ((strFile = br.readLine()) != null){
                //默认是没有换行符
                //bw.write(strFile);
                bw.write(strFile + "\n");
            }

        } 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();
                }
            }
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值