1.基本需求
- 源目录有的文件,目标目录必须有
- 源目录没有的文件,但目标目录有,称为非法文件,需要执行删除
- 文件删除后,反向递归执行空目录删除
2.扩展需求
3.代码实现
package www.cuit.edu.cn.other;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class IncrementalFileMigration {
public static String sourcePathPrefix = null;
public static String targetPathPrefix = null;
public static void main(String[] args) {
}
public static void incrementalFileMigration(String sourcePath, String targetPath) {
File sourceFile = new File(sourcePath);
if (!sourceFile.exists()) {
return;
}
List<String> sourceFiles = getAllFiles(sourcePath);
sourceFiles = sourceFiles.parallelStream().map(s -> s.replace(sourcePathPrefix, "")).collect(Collectors.toList());
File targetFile = new File(targetPath);
if (!targetFile.exists()) {
if (targetFile.mkdirs()) {
System.out.println("创建目标目录成功");
} else {
System.out.println("创建目标目录失败");
return;
}
}
List<String> targetFiles = getAllFiles(targetPath);
targetFiles = targetFiles.parallelStream().map(s -> s.replace(targetPathPrefix, "")).collect(Collectors.toList());
Collection<String> intersection = new ArrayList<>();
for (String file : targetFiles) {
if (sourceFiles.contains(file)) {
intersection.add(file);
}
}
Collection<String> waitCopyFiles = new ArrayList<>();
for (String file : sourceFiles) {
if (!intersection.contains(file))
waitCopyFiles.add(file);
}
Collection<String> waitDeleteFiles = new ArrayList<>();
for (String file : targetFiles) {
if (!intersection.contains(file)) {
waitDeleteFiles.add(file);
}
}
doCopy(waitCopyFiles);
doDelete(waitDeleteFiles);
}
private static void doDelete(Collection<String> waitDeleteFiles) {
waitDeleteFiles.parallelStream().forEach(relativePath -> {
File targetAbsolutePath = new File(targetPathPrefix, relativePath);
boolean isDelete = targetAbsolutePath.delete();
if (isDelete) {
System.out.println("删除成功!");
deleteEmptyDir(targetAbsolutePath.getParentFile().getPath(), new File(targetPathPrefix).getPath());
} else {
System.out.println("删除失败!");
}
});
}
private static void deleteEmptyDir(String path, String stopPath) {
File file = new File(path);
File[] files = file.listFiles();
if (path.equals(stopPath) || !file.exists() || (files != null && files.length != 0)) {
return;
}
boolean isDelete = file.delete();
if (isDelete) {
deleteEmptyDir(file.getParentFile().getPath(), stopPath);
}
}
private static void doCopy(Collection<String> waitCopyFiles) {
waitCopyFiles.parallelStream().forEach(relativePath -> {
File originAbsolutePath = new File(sourcePathPrefix, relativePath);
File targetAbsolutePath = new File(targetPathPrefix, relativePath);
File targetParentFile = targetAbsolutePath.getParentFile();
if (!targetParentFile.exists()) {
if (targetParentFile.mkdirs()) {
System.out.println("创建目标目录成功");
} else if (targetParentFile.exists()) {
System.out.println("目录已由其他线程创建!");
}
}
try {
Files.copy(Paths.get(originAbsolutePath.getPath()), Paths.get(targetAbsolutePath.getPath()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.out.println("拷贝文件失败!");
throw new RuntimeException(e);
}
});
}
private static List<String> getAllFiles(String path) {
List<String> paths = new ArrayList<>();
File file = new File(path);
if (file.isFile()) {
paths.add(path);
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
paths.addAll(getAllFiles(f.getPath()));
}
}
return paths;
}
public static void deleteEmptyDirForward(String path,String stopPath) {
File file = new File(path);
if(!file.exists()){
return;
}
File[] files = file.listFiles();
if ((files == null || (files != null && files.length == 0)) && !path.equals(stopPath)) {
boolean isDelete = file.delete();
if (isDelete) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
for (File f : files) {
if (f.isDirectory()) {
deleteEmptyDirForward(f.getPath(),stopPath);
}
}
}
}