import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigLoader {
private Properties properties;
public void load(String configFile) {
try (FileInputStream inputStream = new FileInputStream(configFile)) {
properties = new Properties();
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return properties.getProperty(key);
}
}
public class ConfigManager {
private Properties properties;
private String configFile;
public ConfigManager(String configFile) {
this.configFile = configFile;
}
public void load() {
try (FileInputStream inputStream = new FileInputStream(configFile)) {
properties = new Properties();
properties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public void updateProperty(String key, String value) {
properties.setProperty(key, value);
}
public void save() {
try (FileOutputStream outputStream = new FileOutputStream(configFile)) {
properties.store(outputStream, null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getProperty(String key, String defaultValue) {
String value = properties.getProperty(key);
if (value == null) {
return defaultValue;
}
try {
Integer.parseInt(value);
} catch (NumberFormatException e) {
return defaultValue;
}
return value;
}