diff --git a/src/UE00_Other/duplicateFiles.log b/src/UE00_Other/duplicateFiles.log new file mode 100644 index 0000000..e69de29 diff --git a/src/UE00_Other/findDuplicateFiles.java b/src/UE00_Other/findDuplicateFiles.java new file mode 100644 index 0000000..d3ee41d --- /dev/null +++ b/src/UE00_Other/findDuplicateFiles.java @@ -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> output, String logfile) throws IOException { + for (Map.Entry> 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> duplicatePaths(Path path) { + try (Stream files = Files.walk(path)) { + Map> duplicates = new HashMap<>(); + files.filter(Files::isRegularFile).forEach(file -> { + List temp = duplicates.getOrDefault(file.getFileName(), new ArrayList<>()); + temp.add(file); + duplicates.put(file.getFileName(), temp); + }); + return duplicates; + } catch (IOException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file