import java.io.*;
import java.util.*;
public class DuplicateRemover {
public static void removeDuplicates(String inputFilePath, String outputFilePath, boolean dryRun) throws IOException {
// Use a Set to store unique lines. Set automatically handles duplicates.
Set<String> uniqueLines = new HashSet<>();
try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
if (uniqueLines.add(line)) { // add() returns true if element was not already present
writer.write(line);
writer.newLine();
}
}
if(dryRun){
System.out.println("Dry run mode is active. No changes were made.");
} else {
System.out.println("Duplicates removed. Output written to: " + outputFilePath);
}
}
}
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Usage: java DuplicateRemover <input_file_path> <output_file_path> [dryRun (true/false)]");
return;
}
String inputFilePath = args[0];
String outputFilePath = args[1];
boolean dryRun = false;
if(args.length == 3){
dryRun = args[2].equalsIgnoreCase("true");
}
try {
removeDuplicates(inputFilePath, outputFilePath, dryRun);
} catch (IOException e) {
System.err.println("Error processing files: " + e.getMessage());
e.printStackTrace();
}
}
}
Add your comment