import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class LogFileHasher {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java LogFileHasher <log_file_path>");
return;
}
String logFilePath = args[0];
String hash = calculateLogFileHash(logFilePath);
if (hash != null) {
System.out.println("Log file hash: " + hash);
} else {
System.err.println("Error processing log file: " + logFilePath);
}
}
public static String calculateLogFileHash(String logFilePath) {
HashMap<String, Integer> charCounts = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(logFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
charCounts.put(String.valueOf(c), charCounts.getOrDefault(String.valueOf(c), 0) + 1);
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
return null; // Indicate error
}
// Simple hash calculation - sum of character counts (can be improved)
int hash = 0;
for (int count : charCounts.values()) {
hash += count;
}
return String.valueOf(hash);
}
}
Add your comment