读取proprtties工具类

来源:互联网 发布:程序员和产品经理关系 编辑:程序博客网 时间:2024/05/22 18:22
/*
 * 两种方式
 * InputStream inStream =  this.getClass().getResourceAsStream("/xx.properties");//普通类好用
   InputStream inStream = 类名.class.getResourceAsStream("/xxx.properties");//静态类好用
 */
    /**
     * 根据key取得类路径下properFilePath文件中的value值
     * @param properFilePath  pro文件的path   
     * @param key
     * @return
     */
    public static String getProKey(String properFilePath,String key){
        String value="";
        InputStream inStream =null;
            try {
                inStream = ReadPropertiesUtil.class.getResourceAsStream(properFilePath);
                Properties prop = new Properties();  
                prop.load(inStream);  
                value=prop.getProperty(key);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                if(inStream!=null){
                    try {
                        inStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            //Object [] fmtargs = {""};//替换string中的{0}
            //String content =MessageFormat.format (registerMessage, fmtargs);
        return value;
    }
原创粉丝点击