import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author Sam
* @Description: 讀取配置工具類
* @date 2018/5/23
*/
public class PropertiesUtils {
private static Properties props;
static {
loadProps();
}
synchronized static private void loadProps() {
props = new Properties();
InputStream in = null;
try {
// 要加載的屬性文件
in = PropertiesUtils.class.getResourceAsStream("/config.properties");
props.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String getProperty(String key) {
if (null == props) {
loadProps();
}
return props.getProperty(key);
}
public static String getProperty(String key, String defaultValue) {
if (null == props) {
loadProps();
}
return props.getProperty(key, defaultValue);
}
}
讀取配置工具類
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.