缓存流对非文本文件的复制

@Test
    public void test3(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //节点流
            FileInputStream fis = new FileInputStream("D:\\java笔记.docx");
            FileOutputStream fos = new FileOutputStream("D:\\java笔记2.docx");
            //缓存流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            byte[] cbuf = new byte[1024];
            int len;
            while ((len = bis.read(cbuf))!=-1){
                bos.write(cbuf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭外层流的同时,内层流也会自动的进行关闭,内层流关闭可以省略
            if(bis!=null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis!=null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

  

posted @ 2022-09-01 20:51  iTao0128  阅读(15)  评论(0)    收藏  举报