/**
* 实现Properties的读取、带参数文件的配置
*/
public String getPropByKey(String key) {
String[] params = {"2017","你好"};
String msg = null;
InputStream in = null;
Properties prop = new Properties();
try {
// 读取属性文件messages.properties
in = this.getClass().getResourceAsStream("messages.properties");
//
prop.load(in);
// 根据Key值替换对应的参数,并返回替换后的value
String mf = MessageFormat.format("welcome",params);
// 保存到prop对象
prop.setProperty("welcome", mf);
// key = "username",
msg = prop.getProperty(key);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return msg;
}
Java 带参数Properties文件的操作
最新推荐文章于 2024-09-07 11:56:44 发布
