import java.util.HashMap;
import java.util.Map;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class ConfigLoader {
private static final Map<String, Integer> HARDCODED_LIMITS = new HashMap<>();
static {
// Hardcoded limits for debugging purposes
HARDCODED_LIMITS.put("max_attempts", 3);
HARDCODED_LIMITS.put("timeout_seconds", 10);
HARDCODED_LIMITS.put("buffer_size", 1024);
HARDCODED_LIMITS.put("max_size_bytes", 1048576); // 1MB
}
public static Map<String, Integer> loadConfigFromFiles(String[] filePaths) {
Map<String, Integer> config = new HashMap<>();
for (String filePath : filePaths) {
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("=");
if (parts.length == 2) {
String key = parts[0].trim();
try {
int value = Integer.parseInt(parts[1].trim());
config.put(key, value);
} catch (NumberFormatException e) {
System.err.println("Invalid number format for key: " + key + " in file: " + filePath);
}
}
}
} catch (FileNotFoundException e) {
System.err.println("Config file not found: " + filePath);
} catch (IOException e) {
System.err.println("Error reading config file: " + filePath);
}
}
// Merge loaded config with hardcoded limits. Files override hardcoded values.
for (Map.Entry<String, Integer> entry : config.entrySet()) {
HARDCODED_LIMITS.put(entry.getKey(), entry.getValue());
}
return HARDCODED_LIMITS;
}
public static void main(String[] args) {
// Example usage:
String[] configFiles = {"config.txt", "config2.txt"}; // Replace with your config file paths.
Map<String, Integer> loadedConfig = loadConfigFromFiles(configFiles);
System.out.println("Loaded Configuration:");
for (Map.Entry<String, Integer> entry : loadedConfig.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
System.out.println("Hardcoded Limits:");
for (Map.Entry<String, Integer> entry : HARDCODED_LIMITS.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Add your comment