import java.util.HashMap;
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class TimestampConfigReader {
private static final String DEFAULT_TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
public static Map<String, String> readTimestampConfig(String configFilePath) throws IOException {
Map<String, String> timestampConfig = new HashMap<>();
try (BufferedReader reader = new BufferedReader(new FileReader(configFilePath))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim(); // Remove leading/trailing whitespace
// Skip empty lines and comments
if (line.isEmpty() || line.startsWith("#")) {
continue;
}
// Parse the line. Assumes key=value format.
String[] parts = line.split("=", 2); //Split into key and value
if (parts.length == 2) {
String key = parts[0].trim();
String value = parts[1].trim();
timestampConfig.put(key, value);
} else {
System.err.println("Invalid line format: " + line); //Log errors
}
}
} catch (IOException e) {
System.err.println("Error reading config file: " + e.getMessage());
throw e; //Re-throw exception
}
//Apply default timestamp format if not specified
if (!timestampConfig.containsKey("timestamp_format")) {
timestampConfig.put("timestamp_format", DEFAULT_TIMESTAMP_FORMAT);
}
return timestampConfig;
}
public static void main(String[] args) {
try {
//Example usage:
Map<String, String> config = readTimestampConfig("timestamp_config.txt");
System.out.println(config);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Add your comment