This commit is contained in:
2025-05-08 20:22:48 +02:00
parent f65a95e5ac
commit 0ebb73d035
2 changed files with 45 additions and 0 deletions

View File

View File

@@ -0,0 +1,45 @@
package UE00_Other;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public class findDuplicateFiles {
private static final String logfile = "src/UE00_Other/duplicateFiles.log";
public static void main(String[] args) throws IOException {
try (FileWriter _ = new FileWriter(logfile, false)) {
System.out.println("File contents cleared.");
}
writeToFile(duplicatePaths(Path.of("S:/Musik")), logfile);
}
public static void writeToFile(Map<Path, List<Path>> output, String logfile) throws IOException {
for (Map.Entry<Path, List<Path>> entry : output.entrySet())
if (entry.getValue().size() > 1) try (FileWriter fw = new FileWriter(logfile, true)) {
StringBuilder text = new StringBuilder(entry.getKey() + ":\n");
for (Path path : entry.getValue()) text.append("\t").append(path).append("\n");
fw.write(text.append("\n").toString());
}
}
public static Map<Path, List<Path>> duplicatePaths(Path path) {
try (Stream<Path> files = Files.walk(path)) {
Map<Path, List<Path>> duplicates = new HashMap<>();
files.filter(Files::isRegularFile).forEach(file -> {
List<Path> temp = duplicates.getOrDefault(file.getFileName(), new ArrayList<>());
temp.add(file);
duplicates.put(file.getFileName(), temp);
});
return duplicates;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}