首页 > 其他分享 >BufferedOutputStream

BufferedOutputStream

时间:2023-01-25 19:44:32浏览次数:42  
标签:BufferedInputStream bos BufferedOutputStream new null bis

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

标签:BufferedInputStream,bos,BufferedOutputStream,new,null,bis
From: https://www.cnblogs.com/cyyyds/p/17067198.html

相关文章

  • BufferedOutPutStream字节输出流
    字节输出流构造方法:BufferedOutPutStream(OutPutStreamout)BufferedOutPutStream(OutPutStreamout,intsize)参数:OutPutStreamout:字节输出流,可以传递OutPutS......