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

来源:互联网 发布:传奇db数据库编辑器 编辑:程序博客网 时间:2024/06/05 19:10
//单例模式实现读取***.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);    }}
0 0