zoukankan      html  css  js  c++  java
  • JAVA IO 缓冲流

    摘抄自 b站尚硅谷JAVA视频教程

     BufferedInputStream  BufferedOutputStream  缓冲字节流

    BufferedReader  BufferedWriter 缓冲字符流

    用法跟无缓冲的IO实现类一致. 其中

      输出流增加了一个flush方法,用于手动刷新缓冲区.

      字符缓冲输入流(BufferedReader)增加了一个readline方法,返回值为null时为文件末尾

      字符缓冲输出流(BufferWriter)增加了一个newline方法,可以写入一个换行符

    在字节流外面,包一层缓冲流,实现读写的加速. 因为其内部增加了一个缓冲区.

    关闭的时候,仅关闭缓冲流即可.(会自动关闭被包裹的字节流)

    File file = null;
            File gg = null;
            gg = new File("gg.jpg");
            file = new File("ggCopy.jpg");
            FileOutputStream fo=null;
            FileInputStream fi =null;
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                fo = new FileOutputStream(file);
                bos = new BufferedOutputStream(fo);
                fi = new FileInputStream(gg);
                bis = new BufferedInputStream(fi);
    
                byte [] bytes = new byte[5];
                int len =0;
                while ((len=bis.read(bytes))!=-1){
                    bos.write(bytes,0,len);
                    ;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(bos!=null){
                    try{
                        bos.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
                if(bis!=null){
                    try{
                        bis.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
  • 相关阅读:
    53. Maximum Subarray
    64. Minimum Path Sum
    28. Implement strStr()
    26. Remove Duplicates from Sorted Array
    21. Merge Two Sorted Lists
    14. Longest Common Prefix
    7. Reverse Integer
    412. Fizz Buzz
    linux_修改域名(centos)
    linux_redis常用数据类型操作
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/12341248.html
Copyright © 2011-2022 走看看