// 排除特定文件夹
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;
}
}

被折叠的 条评论
为什么被折叠?



