将一个文件夹里的文件包括文件夹 copy另一个文件夹里

/**
     * 文件夹复制
     * @param filePath
     * @return
     */
    public static void copyFiles(File srcDir,File toDir) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            if(!toDir.exists()) {
                //创建目标文件夹
                toDir.mkdir();
            }

            //得到该文件夹下所有的文件
            File[] files = srcDir.listFiles();
            for (File file2 : files) {
                //如果是文件
                if(file2.isFile()) {
                    bis = new BufferedInputStream(new FileInputStream(file2));
                    //根据目标路径和文件名构建目标文件
                    File toFile = new File(toDir, file2.getName());
                    byte[] arr = new byte[1024*8];
                    bis.read(arr);

                    bos = new BufferedOutputStream(new FileOutputStream(toFile));
                    bos.write(arr);

                }else {//如果是文件夹
                    //根据原有的目标文件夹路径加上新的文件夹名字  创建新的目标文件夹
                    File newDir = new File(toDir, file2.getName());
                    //递归调用
                    copyFiles(file2, newDir);
                }
            }



        } catch (Exception e) {

        } finally {
            if(bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值