1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. public class LogPrinter {
  9. public static void main(String[] args) {
  10. String logFilePath = "default.log"; // Default log file path
  11. Map<String, Integer> defaultValues = new HashMap<>();
  12. defaultValues.put("counter", 0);
  13. defaultValues.put("total", 0);
  14. defaultValues.put("errorCount", 0);
  15. try {
  16. printLogFile(logFilePath, defaultValues);
  17. } catch (IOException e) {
  18. System.err.println("Error reading log file: " + e.getMessage());
  19. }
  20. }
  21. public static void printLogFile(String logFilePath, Map<String, Integer> defaultValues) throws IOException {
  22. String line;
  23. BufferedReader reader = new BufferedReader(new FileReader(logFilePath));
  24. List<Map<String, Integer>> logEntries = new ArrayList<>();
  25. //Read the log file line by line
  26. while ((line = reader.readLine()) != null) {
  27. // Parse each line (assuming a simple key=value format)
  28. String[] parts = line.split("=", 2);
  29. if (parts.length == 2) {
  30. String key = parts[0].trim();
  31. String valueStr = parts[1].trim();
  32. //Try to parse the value as an integer, use default if parsing fails
  33. Integer value = Integer.parseInt(valueStr);
  34. if(value != Integer.MIN_VALUE){
  35. logEntries.add(new HashMap<>());
  36. logEntries.get(logEntries.size() - 1).put(key, value);
  37. } else {
  38. logEntries.add(new HashMap<>());
  39. logEntries.get(logEntries.size() - 1).put(key, defaultValues.get(key));
  40. }
  41. }
  42. }
  43. reader.close();
  44. // Pretty print the results
  45. printLogEntries(logEntries, defaultValues);
  46. }
  47. private static void printLogEntries(List<Map<String, Integer>> logEntries, Map<String, Integer> defaultValues) {
  48. //Print Log Entries
  49. for (Map<String, Integer> entry : logEntries) {
  50. System.out.println("--------------------");
  51. for (Map.Entry<String, Integer> pair : entry.entrySet()) {
  52. System.out.println(pair.getKey() + ": " + pair.getValue());
  53. }
  54. }
  55. }
  56. }

Add your comment