You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package com.community.pocket.util;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
//读取配置文件工具类
|
|
|
|
public class PropertiesUtil {
|
|
|
|
|
|
|
|
private static Properties properties;
|
|
|
|
|
|
|
|
private final static Map<String, String> values = new HashMap<>();
|
|
|
|
|
|
|
|
// 文件路径
|
|
|
|
private static final String filePath = "/assets/config.properties";
|
|
|
|
|
|
|
|
static {
|
|
|
|
properties = new Properties();
|
|
|
|
try {
|
|
|
|
InputStream is = PropertiesUtil.class.getResourceAsStream(filePath);
|
|
|
|
properties.load(is);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取字符串配置
|
|
|
|
public static String getValue(String key) {
|
|
|
|
if (values.containsKey(key)) {
|
|
|
|
return values.get(key);
|
|
|
|
} else {
|
|
|
|
String value = properties.getProperty(key);
|
|
|
|
values.put(key, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 获取整数配置
|
|
|
|
public static int getIntValue(String key) {
|
|
|
|
String value = getValue(key);
|
|
|
|
if (value.matches("\\d+")) {
|
|
|
|
return Integer.parseInt(value);
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException("值转换异常!无法把" + key + "=" + value + "转化成整数");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|