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;
}
}