言归正传,直接看代码
public class RemoveDuplicateFiles {
public static void main(String[] args) throws IOException {
String directoryPath = "D:\\dir";
List<File> allFiles = getAllFiles(directoryPath);
removeDuplicateFile(allFiles);
}
private static void removeDuplicateFile(List<File> allFiles) throws IOException {
// 按文件大小排序
allFiles.sort(Comparator.comparing(File::length));
// 找出文件大小相同的文件
for (int i = 0; i < allFiles.size(); i++) {
File file = allFiles.get(i);
byte[] fileContent = getFileContent(file);
for (int j = i+1;j<allFiles.size();j++){
File file1 = allFiles.get(j);
// 如果文件大小相同则比较内容
if (file.length() == file1.length()){
byte[] fileContent1 = getFileContent(file1);
// 比较字节内容,如果内容也想通,则删除重复的文件
if (Arrays.equals(fileContent,fileContent1)){
file1.delete();
}
}else {
i=j;
break;
}
}
}
}
private static byte[] getFileContent(File file) throws IOException {
Path path = Paths.get(file.getAbsolutePath());
return Files.readAllBytes(path);
}
private static List<File> getAllFiles(String directoryPath) {
List<File> allFiles = new ArrayList<>();
File directory = new File(directoryPath);
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()){
String absolutePath = file.getAbsolutePath();
List<File> allFiles1 = getAllFiles(absolutePath);
allFiles.addAll(allFiles1);
}else {
allFiles.add(file);
}
}
return allFiles;
}
}