1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class LogFileHasher {
  7. public static void main(String[] args) {
  8. if (args.length != 1) {
  9. System.out.println("Usage: java LogFileHasher <log_file_path>");
  10. return;
  11. }
  12. String logFilePath = args[0];
  13. String hash = calculateLogFileHash(logFilePath);
  14. if (hash != null) {
  15. System.out.println("Log file hash: " + hash);
  16. } else {
  17. System.err.println("Error processing log file: " + logFilePath);
  18. }
  19. }
  20. public static String calculateLogFileHash(String logFilePath) {
  21. HashMap<String, Integer> charCounts = new HashMap<>();
  22. try (BufferedReader reader = new BufferedReader(new FileReader(logFilePath))) {
  23. String line;
  24. while ((line = reader.readLine()) != null) {
  25. for (int i = 0; i < line.length(); i++) {
  26. char c = line.charAt(i);
  27. charCounts.put(String.valueOf(c), charCounts.getOrDefault(String.valueOf(c), 0) + 1);
  28. }
  29. }
  30. } catch (IOException e) {
  31. System.err.println("Error reading file: " + e.getMessage());
  32. return null; // Indicate error
  33. }
  34. // Simple hash calculation - sum of character counts (can be improved)
  35. int hash = 0;
  36. for (int count : charCounts.values()) {
  37. hash += count;
  38. }
  39. return String.valueOf(hash);
  40. }
  41. }

Add your comment