// 资源目录的根目录下 适应于web项目 : 也可以使用加载进类加载器的方式 ,推荐使用一种最简单的方法:
ResourceBundle bundle = ResourceBundle.getBundle("xxx"); // xxx为 xxx.properties的名称
//例如:redis.properties : ResourceBundle bundle = ResourceBundle.getBundle("redis");
String key= bundle.getString("key");
//-----------------------------------------------------------------
// 拓展: 绝对路径获取:常用于客户端的配置文件 : 例如swing程序 打包成jar包后再打包成exe,
// src目录就读取不到了 这时需要读取该程序所在的绝对路径
public Map getXXX() {
InputStream is = null;
try {
// 核心代码
is = new FileInputStream(System.getProperty("user.dir") + "\\xxx.properties");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Properties prop = new Properties();
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
// 也可以直接prop.get(key)
Map<String, String> xxMap = new HashMap(prop);
return xxMap;
}
java web项目最简单的配置文件取值方法 不再惧怕写配置文件
最新推荐文章于 2023-02-22 10:46:19 发布