【工具】读取proprtties工具类

获取properties内容:

基本的使用看网络上大多是这样的,使用时注意线程安全以及读写的实时性问题。

 

1、直接通过流读取(反射):

InputStream inStream =  this.getClass().getResourceAsStream("/xx.properties");//普通类

InputStream inStream = 类名.class.getResourceAsStream("/xxx.properties");//静态类

eg:

    /**
     * 根据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;
    }

2、大佬总结的五种方式:https://www.cnblogs.com/hafiz/p/5876243.html

 

posted @ 2017-07-08 08:24  黑猫先生  阅读(255)  评论(0编辑  收藏  举报