1. import java.util.HashMap;
  2. import java.util.Map;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. public class TimestampConfigReader {
  7. private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
  8. public static Map<String, String> readTimestampConfig(String configFilePath) throws IOException {
  9. Map<String, String> timestampConfig = new HashMap<>();
  10. try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
  11. String line;
  12. while ((line = reader.readLine()) != null) {
  13. line = line.trim(); // Remove leading/trailing whitespace
  14. // Skip empty lines and comments
  15. if (line.isEmpty() || line.startsWith("#")) {
  16. continue;
  17. }
  18. // Parse the line. Assumes key=value format.
  19. String[] parts = line.split("=", 2); //Split into key and value
  20. if (parts.length == 2) {
  21. String key = parts[0].trim();
  22. String value = parts[1].trim();
  23. timestampConfig.put(key, value);
  24. } else {
  25. System.err.println("Invalid line format: " + line); //Log errors
  26. }
  27. }
  28. } catch (IOException e) {
  29. System.err.println("Error reading config file: " + e.getMessage());
  30. throw e; //Re-throw exception
  31. }
  32. //Apply default timestamp format if not specified
  33. if (!timestampConfig.containsKey("timestamp_format")) {
  34. timestampConfig.put("timestamp_format", DEFAULT_TIMESTAMP_FORMAT);
  35. }
  36. return timestampConfig;
  37. }
  38. public static void main(String[] args) {
  39. try {
  40. //Example usage:
  41. Map<String, String> config = readTimestampConfig("timestamp_config.txt");
  42. System.out.println(config);
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }

Add your comment