1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.*;
  5. public class CookieDiff {
  6. public static void main(String[] args) {
  7. if (args.length != 2) {
  8. System.out.println("Usage: java CookieDiff <file1.txt> <file2.txt>");
  9. return;
  10. }
  11. String file1 = args[0];
  12. String file2 = args[1];
  13. Map<String, String> cookies1 = loadCookies(file1);
  14. Map<String, String> cookies2 = loadCookies(file2);
  15. System.out.println("Differences between " + file1 + " and " + file2 + ":");
  16. // Find cookies present in file1 but not in file2
  17. Map<String, String> diff1 = findDiff(cookies1, cookies2);
  18. if (!diff1.isEmpty()) {
  19. System.out.println("Cookies only in " + file1 + ":");
  20. for (Map.Entry<String, String> entry : diff1.entrySet()) {
  21. System.out.println(entry.getKey() + "=" + entry.getValue());
  22. }
  23. }
  24. // Find cookies present in file2 but not in file1
  25. Map<String, String> diff2 = findDiff(cookies2, cookies1);
  26. if (!diff2.isEmpty()) {
  27. System.out.println("Cookies only in " + file2 + ":");
  28. for (Map.Entry<String, String> entry : diff2.entrySet()) {
  29. System.out.println(entry.getKey() + "=" + entry.getValue());
  30. }
  31. }
  32. }
  33. private static Map<String, String> loadCookies(String filename) {
  34. Map<String, String> cookies = new HashMap<>();
  35. try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
  36. String line;
  37. while ((line = reader.readLine()) != null) {
  38. String[] parts = line.split("=");
  39. if (parts.length == 2) {
  40. cookies.put(parts[0].trim(), parts[1].trim());
  41. }
  42. }
  43. } catch (IOException e) {
  44. System.err.println("Error reading file: " + filename);
  45. return new HashMap<>(); // Return empty map in case of error
  46. }
  47. return cookies;
  48. }
  49. private static Map<String, String> findDiff(Map<String, String> map1, Map<String, String> map2) {
  50. Map<String, String> diff = new HashMap<>();
  51. for (Map.Entry<String, String> entry : map1.entrySet()) {
  52. if (!map2.containsKey(entry.getKey()) || !entry.getValue().equals(map2.get(entry.getKey()))) {
  53. diff.put(entry.getKey(), entry.getValue());
  54. }
  55. }
  56. return diff;
  57. }
  58. }

Add your comment