方法:一个简单的读取配置文件.properties的工具类 JAVA

时间:2024-03-23 14:35:56

import java.util.ResourceBundle;

public class ConfigHelper {
private static ConfigHelper instance;

private ConfigHelper() {

}

public static ConfigHelper getInstance() {
if (instance == null) {
synchronized (ConfigHelper.class) {
if (instance == null) {
instance = new ConfigHelper();
}
}
}
return instance;
}
public ResourceBundle getConfigResource(String propertyName){
ResourceBundle rb = ResourceBundle.getBundle(propertyName.trim());
return rb;
}
}

XXX.properties配置文件可以放在resource目录下。

XXX.properties配置文件内容为:属性名=属性值

如config.properties:

#CPU权重

cpuWeight=0.5

#内存权重
memoryWeight=0.3

#磁盘权重
diskWeight=0.2

读取某个配置文件中的某个属性值:

String cpuWeight = ConfigHelper.getInstance().getConfigResource("config").getString("cpuWeight")