zoukankan      html  css  js  c++  java
  • 【工具】读取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

  • 相关阅读:
    链式栈的C++实现
    Java面试之设计模式二
    前端资源
    Java面试之异常
    Java面试之序列化
    Java面试之重写(Override)与重载(Overload)
    项目视图展示
    Java面试之集合
    Java面试之SSH框架面试题集锦
    JDBC技术
  • 原文地址:https://www.cnblogs.com/the-fool/p/11054221.html
Copyright © 2011-2022 走看看