随笔 - 43  文章 - 0  评论 - 0  阅读 - 3020

BufferedOutputStream

BufferedOutputStream

BufferedOutputStream是字节流,实现缓冲的输出流,可以将多个字节写入底层输出流中,而不必对每次字节写入调用底层系统

应用实例

完成音乐/图片 的拷贝(要求使用BufferedOutputStream)

//演示使用BufferedOutputStream 和 BufferedInputStream 使用
public class BufferedCopy2_ {
    public static void main(String[] args) {

        String srcFilePath = "d:\\pic.JPG";
        String destFilePath = "d:\\pic2.JPG";

        //创建BufferedOutputStream对象和BufferedInputStream对象
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            //因为FileInputStream继承了InputStream
            bis = new BufferedInputStream(new FileInputStream(srcFilePath));
            bos = new BufferedOutputStream(new FileOutputStream(destFilePath));

            //循环读取文件,并写入到destFilePath
            byte[] buff = new byte[1024];
            int readLen = 0;
            //当返回-1时表示文件读取完毕
            while ((readLen = bis.read(buff)) != -1) {
                bos.write(buff,0,readLen);
            }
            System.out.println("文件拷贝成功···");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流,关闭外层的处理流即可,底层会去关闭节点流
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
posted on   小宇不会编程  阅读(1215)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示