文件流之字节缓冲流(BufferedInputStream BufferedOutputStream)

  • 缓冲流的优势是: 

  BufferedInputStream: 输入缓冲流:先从磁盘中将要读取的内容放入到内存中,再一次性从内存中取出来,避免了读一段取一段;

  BufferedOutputStream: 输出缓冲流:先将要输出的内容放入到内存中,再一次性全都输出。

  • 缓冲流的构造函数:
public BufferedInputStream(InputStream in) { this (in, DEFAULT_BUFFER_SIZE);  }
public BufferedInputStream(InputStream in, int size) { super (in); if (size <= 0 ) { throw  new IllegalArgumentException("Buffer size <= 0" );  }  buf = new  byte [size];  }
public BufferedOutputStream(OutputStream out) { this (out, 8192 );  }
public BufferedOutputStream(OutputStream out, int size) { super (out); if (size <= 0 ) { throw  new IllegalArgumentException("Buffer size <= 0" );  }  buf = new  byte [size];  }
  • 缓冲流关闭:

只需将缓冲流关闭,无需将节点流关闭,因为缓冲流内部会自动调用节点流关闭。

  • 缓冲流需要调用flush()方法才能将内容输入或输出出来,或者调用close()方法时,也会调用flush()方法。
  • 注意:

    new FileOutputStream("***"); 如果参数中的路径有目录不存在,并不会自动创建目录;

    此外:调用File 类的mkdir()或mkdirs()可能出现错误“Access is denied”,暂时还没解决。

  • 缓冲流的应用
public class BufferedStream {

    public static void main(String[] args) {
        //BufferedStream fbs = new BufferedStream();
        // fbs.writeFileWithBufferedStream("HelloBufferedOutputStream",
        // "/home/rding/bufferd.txt");
        // fbs.readFileWithBufferedStream("/home/rding/bufferd.txt");
        //fbs.writeFileWithBufferedStream("HelloWorld", "d:/rding/rding/buffered.txt");
        
        String str = "c:\\cc\\cc";
        System.out.println(str.lastIndexOf("\\"));
    }

    public void readFileWithBufferedStream(String srcPath, int size) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        StringBuffer sb = new StringBuffer();
        try {
            inputStream = new FileInputStream(srcPath);
            bufferedInputStream = new BufferedInputStream(inputStream);
            byte[] byteArr = new byte[size];
            int len = 0;
            while ((len = bufferedInputStream.read(byteArr)) != -1) {
                sb.append(new String(byteArr), 0, len);
            }
            System.out.println(sb);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void readFileWithBufferedStream(String srcPath) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;
        try {
            inputStream = new FileInputStream(srcPath);
            bufferedInputStream = new BufferedInputStream(inputStream);
            int size = bufferedInputStream.available();
            byte[] byteArr = new byte[size];
            bufferedInputStream.read(byteArr);
            String str = new String(byteArr);
            System.out.println(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void writeFileWithBufferedStream(String content, String destPath) {
        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {

            // 下面的语句中,如果destPath中的目录不存在,不会自动创建目录,因此,建议使用下面的代码
            // outputStream = new FileOutputStream(destPath);
       // 前提是输出的路径是"/"而不是"\\"
            File tmp = new File(destPath.substring(0, destPath.lastIndexOf("/")));
            if(!tmp.exists()){
                tmp.mkdirs();
            }
            outputStream = new FileOutputStream(destPath);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

            byte[] byteArr = content.getBytes();
            bufferedOutputStream.write(byteArr);
            bufferedOutputStream.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    public void copyFileWithBufferedStream(String srcPath, String destPath, int size) {
        InputStream inputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            inputStream = new FileInputStream(srcPath);

            // 下面的语句中,如果destPath中的目录不存在,不会自动创建目录,因此,建议使用下面的代码
            // outputStream = new FileOutputStream(destPath);

            File tmp = new File(destPath.substring(0, destPath.lastIndexOf("/")));
            if(!tmp.exists()){
                tmp.mkdirs();
            }
            
            outputStream = new FileOutputStream(tmp);

            bufferedInputStream = new BufferedInputStream(inputStream);

            bufferedOutputStream = new BufferedOutputStream(outputStream);

            byte[] byteArr = new byte[size];

            int len = 0;
            while ((len = bufferedInputStream.read(byteArr)) != -1) {
                bufferedOutputStream.write(byteArr, 0, len);
            }
            bufferedOutputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}

 

转载于:https://www.cnblogs.com/lfdingye/p/7520448.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值