import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Main {
private static Set<String> samefileSet = null;
private static String fileSeparator = "/";
public static void main(String[] args) {
String parentPath = "F:\\pre";
String newFilePath = "F:\\new";
//更新的文件存放的路径
List<String> newFiles = getFileNamesByFilePath(newFilePath);
//获取配置文件工程下的文件夹
List<String> filePaths = getFilePathByFilePath(parentPath);
if (filePaths.isEmpty()) {
System.out.println("该目录下没有文件夹");
}
//循环目录下的文件
filePaths.stream().forEach(filePath -> {
//获取路径下的所有文件
List<String> fileNames = getFileNamesByFilePath(filePath);
//得到要替换的文件
samefileSet = getSameFile(fileNames, newFiles);
samefileSet.stream().forEach(newFileName -> {
fileNames.stream().forEach(oldFileName -> {
if (oldFileName.equals(newFileName)) {
String sourcePath = newFilePath + fileSeparator + newFileName;
String destPath = filePath + fileSeparator + oldFileName;
File dest = new File(destPath);
File source = new File(sourcePath);
if (dest.exists()) {
dest.delete();
try {
Path copy = Files.copy(source.toPath(), dest.toPath());
System.out.println("更新文件"+copy.toString()+"成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
});
});
}
/**
* 获取相同的文件(此次要替换的文件)
*
* @param fileNames
* @param newFiles
* @return
*/
private static Set<String> getSameFile(List<String> fileNames, List<String> newFiles) {
Set<String> newFileSet = new HashSet(newFiles);
Set<String> fileNamesSet = new HashSet(fileNames);
fileNamesSet.retainAll(newFileSet);
return fileNamesSet;
}
/**
* 以文件的形式来获取包下的所有 .properties 结尾的文件
*
* @param filePath
*/
public static List<String> getFileNamesByFilePath(String filePath) {
List<String> fileNames = new ArrayList<>();
// 获取此包的目录 建立一个File
File dir = new File(filePath);
// 如果不存在或者 也不是文件就直接返回
if (!dir.exists() || !dir.isDirectory()) {
return fileNames;
}
// 如果存在 就获取包下的所有文件 包括目录
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.properties结尾的文件
public boolean accept(File file) {
return (file.getName().endsWith(".properties"));
}
});
// 循环所有文件
for (File file : dirfiles) {
// 如果是java类文件 去掉后面的.class 只留下类名
String className = file.getName();
// 添加到集合中去
fileNames.add(className);
}
return fileNames;
}
/**
* 获取该目录下所有的文件夹
*
* @param filePath
*/
public static List<String> getFilePathByFilePath(String filePath) {
List<String> filePaths = new ArrayList<String>();
// 获取此包的目录 建立一个File
File dir = new File(filePath);
// 如果不存在或者 也不是文件就直接返回
if (!dir.exists() || !dir.isDirectory()) {
return filePaths;
}
// 如果存在 就获取包下的所有文件 包括目录
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定义过滤规则 如果可以循环(包含子目录) 或则是以.properties结尾的文件
public boolean accept(File file) {
return (file.isDirectory());
}
});
// 循环所有文件
for (File file : dirfiles) {
// 如果是java类文件 去掉后面的.class 只留下类名
String className = file.getAbsolutePath();
try {
String canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
}
// 添加到集合中去
filePaths.add(className);
}
return filePaths;
}
}