递归复制文件夹

递归:每一级函数去调用自身,但每次调用函数都有自己的变量(以某种方式缩小问题的规模),并且每一次函数的调用都会有一次返回,位于递归调用前的语句(一般是递归出口)和各级被调用函数间具有相同的执行顺序,位于递归调用后的语句的执行顺序和各个被调用函数的顺序相反,当程序执行完最后一层递归调用后,参数变量满足了递归出口的条件就开始逐级返回。虽然每一级递归都有自己的变量,但是函数代码并不会得到复制。
 

public class Test1 {
    public static void main(String[] args) {
        copy(new File("D:\\idea_code"),new File("D:\\idea20333"));
    }

    /**
     *    复制
     * @param src   原文件
     * @param dest  目标文件
     */
    public static void copy(File src, File dest){
        // 判断源文件目录是否存在
        if (src != null && src.exists() && src.isDirectory()) {
            // 目标目录需要创建一下
            File destOne = new File(dest,src.getName());
            destOne.mkdirs();
            // 提取原目录下的全部一级文件对象
            File[] files = src.listFiles();
            // 判断是否存在一级文件对象
            if (files != null && files.length > 0){
                // 遍历一级文件对象
                for (File file : files) {
                    // 判断是文件还是文件夹  是文件直接复制
                    if (file.isFile()){
                        copyFile(file,new File(destOne,file.getName()));
                    }else {
                        // 当前遍历的是文件夹 递归复制
                        copy(file,destOne);
                    }
                }
            }
        }
    }
    /**
     * copy 文件
     * @param srcFile  源文件
     * @param destFile  目标文件
     */
    public static void copyFile(File srcFile,File destFile){
        try(
                // 1.创建一个缓冲字节输入流管道 与原文件接通
                InputStream is = new FileInputStream(srcFile);
                BufferedInputStream bis = new BufferedInputStream(is);
                // 2.创建一个缓冲字节输出流管道与目标文件接通
                OutputStream os = new FileOutputStream(destFile);
                BufferedOutputStream  bos = new BufferedOutputStream(os);
                ){
            // 3.定义一个字节数组转移数据
            byte[] buffer = new byte[1024];
            // 记录每次读取的字节数
            int len ;
            while ((len = bis.read()) != -1) {
                bos.write(buffer,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

以下是一个使用 FatFs 实现递归复制文件夹的示例代码: ```c #include "ff.h" // 递归复制文件夹的函数 FRESULT recursive_copy_folder(const TCHAR *src_path, const TCHAR *dst_path) { FRESULT res; DIR dir; FILINFO fno; TCHAR src_file[_MAX_LFN + 2] = {0}; TCHAR dst_file[_MAX_LFN + 2] = {0}; // 打开源文件夹 res = f_opendir(&dir, src_path); if (res != FR_OK) { return res; } // 创建目标文件夹 res = f_mkdir(dst_path); if (res != FR_OK && res != FR_EXIST) { f_closedir(&dir); return res; } while (1) { // 读取目录项 res = f_readdir(&dir, &fno); if (res != FR_OK) { f_closedir(&dir); return res; } // 若读到的文件名为空,结束循环 if (0 == strlen(fno.fname)) { break; } // 若读到的文件名为当前文件夹或上一级文件夹,跳过 if (0 == strcmp(fno.fname, ".") || 0 == strcmp(fno.fname, "..")) { continue; } // 构造源文件和目标文件的完整路径 memset(src_file, 0, sizeof(src_file)); sprintf((char*)src_file, "%s/%s", src_path, fno.fname); memset(dst_file, 0, sizeof(dst_file)); sprintf((char*)dst_file, "%s/%s", dst_path, fno.fname); if (fno.fattrib & AM_DIR) { // 若是目录,递归复制 res = recursive_copy_folder(src_file, dst_file); if (res != FR_OK) { f_closedir(&dir); return res; } } else { FIL src_fil, dst_fil; // 打开源文件 res = f_open(&src_fil, src_file, FA_READ); if (res != FR_OK) { f_closedir(&dir); return res; } // 创建目标文件 res = f_open(&dst_fil, dst_file, FA_CREATE_ALWAYS | FA_WRITE); if (res != FR_OK) { f_close(&src_fil); f_closedir(&dir); return res; } UINT br, bw; BYTE buffer[4096]; // 复制文件内容 do { res = f_read(&src_fil, buffer, sizeof(buffer), &br); if (res != FR_OK) { break; } if (br > 0) { res = f_write(&dst_fil, buffer, br, &bw); if (res != FR_OK || bw != br) { break; } } } while (br > 0); f_close(&src_fil); f_close(&dst_fil); if (res != FR_OK) { f_closedir(&dir); return res; } } } f_closedir(&dir); return FR_OK; } ``` ### 代码说明 该函数 `recursive_copy_folder` 接收源文件夹路径 `src_path` 和目标文件夹路径 `dst_path` 作为参数。函数首先尝试打开源文件夹和创建目标文件夹,若目标文件夹已存在则忽略错误。然后遍历源文件夹中的所有文件和子文件夹,对于子文件夹递归调用 `recursive_copy_folder` 函数进行复制;对于文件,打开源文件和创建目标文件,将源文件内容逐块复制到目标文件中。 ### 调用示例 ```c int main() { const TCHAR *src_folder = "0:/source_folder"; const TCHAR *dst_folder = "0:/destination_folder"; FRESULT res = recursive_copy_folder(src_folder, dst_folder); if (res == FR_OK) { // 复制成功 } else { // 复制失败,处理错误 } return 0; } ``` ### 注意事项 - 确保 FatFs 库已正确初始化并挂载了相应的存储设备。 - 代码中的缓冲区大小 `4096` 可根据实际情况调整,以平衡内存使用和复制效率。 - 错误处理部分可根据具体需求进行扩展,例如记录错误日志等。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值