import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class CookieDiff {
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java CookieDiff <file1.txt> <file2.txt>");
return;
}
String file1 = args[0];
String file2 = args[1];
Map<String, String> cookies1 = loadCookies(file1);
Map<String, String> cookies2 = loadCookies(file2);
System.out.println("Differences between " + file1 + " and " + file2 + ":");
// Find cookies present in file1 but not in file2
Map<String, String> diff1 = findDiff(cookies1, cookies2);
if (!diff1.isEmpty()) {
System.out.println("Cookies only in " + file1 + ":");
for (Map.Entry<String, String> entry : diff1.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
// Find cookies present in file2 but not in file1
Map<String, String> diff2 = findDiff(cookies2, cookies1);
if (!diff2.isEmpty()) {
System.out.println("Cookies only in " + file2 + ":");
for (Map.Entry<String, String> entry : diff2.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
}
}
}
private static Map<String, String> loadCookies(String filename) {
Map<String, String> cookies = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length == 2) {
cookies.put(parts[0].trim(), parts[1].trim());
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + filename);
return new HashMap<>(); // Return empty map in case of error
}
return cookies;
}
private static Map<String, String> findDiff(Map<String, String> map1, Map<String, String> map2) {
Map<String, String> diff = new HashMap<>();
for (Map.Entry<String, String> entry : map1.entrySet()) {
if (!map2.containsKey(entry.getKey()) || !entry.getValue().equals(map2.get(entry.getKey()))) {
diff.put(entry.getKey(), entry.getValue());
}
}
return diff;
}
}
Add your comment