您的位置:首页 > 产品设计 > UI/UE

根据key从Properties文件中加载指定的value

2017-01-09 15:36 351 查看
//单例模式实现读取***.properties文件的内容
public class OVLoadProperties {
// 声明一个自己的实例
private static OVLoadProperties instance = new OVLoadProperties();
final static String fileName = "/messages_zh_CN.properties";
// 返回该实例
public static synchronized OVLoadProperties getInstance() {
return instance;
}
// 获取key所对应的值
public String getProperties(String key) {
Properties p = new Properties();
InputStream is = null;
try {
// ***.properties文件放在src目录的下边
is = OVLoadProperties.class.getResourceAsStream(fileName);
if (is == null)
is = new FileInputStream(fileName);
p.load(is);
} catch (Exception e) {
System.out.println("加载文件出错啦!" + e.getMessage());
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
return p.getProperty(key);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: