other
This commit is contained in:
0
src/UE00_Other/duplicateFiles.log
Normal file
0
src/UE00_Other/duplicateFiles.log
Normal file
45
src/UE00_Other/findDuplicateFiles.java
Normal file
45
src/UE00_Other/findDuplicateFiles.java
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user