java拷贝文件到另外一个地址(排除一些文件)

// 排除特定文件夹

import java.io.IOException;
import java.nio.file.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class TestFun {
    public static void main(String[] args) {
        System.out.println("开始测试...");
        try {
            FolderCopyExcludeDirs.copyFolderExcludingDirs("D:/fangzheng/wh-fsp", "D:/fangzheng/temp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class FolderCopyExcludeDirs {

    private static final List<String> EXCLUDED_DIRS = Arrays.asList(
            "node_modules", "dist", ".git", ".hbuilderx", ".VSCodeCounter", "ezgo-app-flow-front");

    // 新增:需要排除以该字符串开头的目录和文件
    private static final String DOC_PREFIX_TO_EXCLUDE = "文档--";

    public static void copyFolderExcludingDirs(String sourcePath, String targetPath) throws IOException {
        // 规范化路径
        Path sourceDir = Paths.get(sourcePath).toAbsolutePath().normalize();
        Path targetDir = Paths.get(targetPath).toAbsolutePath().normalize();

        // 检查源目录
        if (!Files.exists(sourceDir)) {
            throw new IOException("源目录不存在: " + sourceDir);
        }
        if (!Files.isDirectory(sourceDir)) {
            throw new IOException("源路径不是目录: " + sourceDir);
        }

        // 防止复制到自身或子目录
        if (targetDir.startsWith(sourceDir)) {
            throw new IOException("目标目录不能是源目录的子目录");
        }

        System.out.println("开始复制...");
        System.out.println("源目录: " + sourceDir);
        System.out.println("目标目录: " + targetDir);
        System.out.println("排除的文件夹: " + EXCLUDED_DIRS);
        System.out.println("排除以 '" + DOC_PREFIX_TO_EXCLUDE + "' 开头的目录和文件");

        // 创建目标目录
        Files.createDirectories(targetDir);

        long startTime = System.currentTimeMillis();
        int[] count = { 0, 0 }; // [复制文件数, 跳过的项目数]

        try (Stream<Path> paths = Files.walk(sourceDir)) {
            paths.forEach(source -> {
                try {
                    Path relativePath = sourceDir.relativize(source);

                    // 跳过空相对路径(源目录自身)
                    if (relativePath.toString().isEmpty()) {
                        return;
                    }

                    // 检查是否排除(新增了 shouldExcludeByPrefix 判断)
                    if (shouldExcludeDirectory(relativePath) || shouldExcludeByPrefix(relativePath)) {
                        System.out.println("跳过: " + relativePath);
                        count[1]++;
                        return;
                    }

                    Path target = targetDir.resolve(relativePath);

                    if (Files.isDirectory(source)) {
                        Files.createDirectories(target);
                    } else {
                        Files.createDirectories(target.getParent());
                        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
                        count[0]++;
                        if (count[0] % 100 == 0) {
                            System.out.println("已复制 " + count[0] + " 个文件...");
                        }
                    }
                } catch (IOException e) {
                    System.err.println("错误处理: " + source + " - " + e.getMessage());
                }
            });
        }

        long endTime = System.currentTimeMillis();
        System.out.println("复制完成!");
        System.out.println("复制文件数: " + count[0]);
        System.out.println("跳过项目数: " + count[1]);
        System.out.println("耗时: " + (endTime - startTime) + "ms");
    }

    /**
     * 原有的排除逻辑:检查是否在排除目录列表中
     */
    private static boolean shouldExcludeDirectory(Path relativePath) {
        for (Path part : relativePath) {
            if (EXCLUDED_DIRS.contains(part.toString())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 新增的排除逻辑:检查是否以 '文档--' 开头
     */
    private static boolean shouldExcludeByPrefix(Path relativePath) {
        for (Path part : relativePath) {
            String partStr = part.toString();
            // 检查目录名或文件名是否以 '文档--' 开头
            if (partStr.startsWith(DOC_PREFIX_TO_EXCLUDE)) {
                return true;
            }
        }
        return false;
    }
}
import java.io.IOException;
import java.nio.file.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class TestFun {
    public static void main(String[] args) {
        System.out.println("开始测试...");
        try {
            FolderCopyExcludeDirs.copyFolderExcludingDirs("D:/fangzheng/gky", "D:/fangzheng/temp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

class FolderCopyExcludeDirs {

    private static final List<String> EXCLUDED_DIRS = Arrays.asList(
            "node_modules", "dist", ".git", ".hbuilderx", ".VSCodeCounter");

    public static void copyFolderExcludingDirs(String sourcePath, String targetPath) throws IOException {
        // 规范化路径
        Path sourceDir = Paths.get(sourcePath).toAbsolutePath().normalize();
        Path targetDir = Paths.get(targetPath).toAbsolutePath().normalize();

        // 检查源目录
        if (!Files.exists(sourceDir)) {
            throw new IOException("源目录不存在: " + sourceDir);
        }
        if (!Files.isDirectory(sourceDir)) {
            throw new IOException("源路径不是目录: " + sourceDir);
        }

        // 防止复制到自身或子目录
        if (targetDir.startsWith(sourceDir)) {
            throw new IOException("目标目录不能是源目录的子目录");
        }

        System.out.println("开始复制...");
        System.out.println("源目录: " + sourceDir);
        System.out.println("目标目录: " + targetDir);
        System.out.println("排除的文件夹: " + EXCLUDED_DIRS);

        // 创建目标目录
        Files.createDirectories(targetDir);

        long startTime = System.currentTimeMillis();
        int[] count = { 0, 0 }; // [复制文件数, 跳过的项目数]

        try (Stream<Path> paths = Files.walk(sourceDir)) {
            paths.forEach(source -> {
                try {
                    Path relativePath = sourceDir.relativize(source);

                    // 跳过空相对路径(源目录自身)
                    if (relativePath.toString().isEmpty()) {
                        return;
                    }

                    // 检查是否排除
                    if (shouldExcludeDirectory(relativePath)) {
                        System.out.println("跳过: " + relativePath);
                        count[1]++;
                        return;
                    }

                    Path target = targetDir.resolve(relativePath);

                    if (Files.isDirectory(source)) {
                        Files.createDirectories(target);
                    } else {
                        Files.createDirectories(target.getParent());
                        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
                        count[0]++;
                        if (count[0] % 100 == 0) {
                            System.out.println("已复制 " + count[0] + " 个文件...");
                        }
                    }
                } catch (IOException e) {
                    System.err.println("错误处理: " + source + " - " + e.getMessage());
                }
            });
        }

        long endTime = System.currentTimeMillis();
        System.out.println("复制完成!");
        System.out.println("复制文件数: " + count[0]);
        System.out.println("跳过项目数: " + count[1]);
        System.out.println("耗时: " + (endTime - startTime) + "ms");
    }

    private static boolean shouldExcludeDirectory(Path relativePath) {
        for (Path part : relativePath) {
            if (EXCLUDED_DIRS.contains(part.toString())) {
                return true;
            }
        }
        return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值