zoukankan      html  css  js  c++  java
  • 使用 java.util.Properties 读取配置文件中的参数

    配置文件格式

    如下的配置参数格式都支持:

    Key = Value
    Key =
    Key:Value
    Key  :Value

    用法

    getProperty方法的返回值是String类型。

            //读取配置文件
            FileInputStream inStream = null;
            try {
                inStream = new FileInputStream("/fetchedfile/redis.conf");
                Properties prop = new Properties();
                prop.load(inStream);
                Field field;
                String property;
            //将配置参数读到对象中
                for(Map.Entry<String, String> entry : RedisConstants.REDIS_PARAM.entrySet()){
                    System.out.println(entry.getKey() + ": " + prop.getProperty(entry.getKey()));
                    field = redisServiceParam.getClass().getDeclaredField(entry.getValue());
                    field.setAccessible(true);
                    //获取参数
                    property = prop.getProperty(entry.getKey());
                    if(null == property || property.isEmpty()){
                        field.set(redisServiceParam, null);
                    }else{
                        field.set(redisServiceParam, property);
                    }
                }
            } catch (IOException | NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }finally {
                if (inStream != null) {
                    try {
                        inStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
  • 相关阅读:
    音乐商店
    sort函数
    优先队列
    畅通工程 并查集,最小生成树,最短路径
    线段树(segment tree )
    bfs
    完全背包
    【Matlab】向图像域添加噪声/高斯/均匀/伽马/指数/椒盐
    【手帐】Bullet Journal教程
    【Matlab】取整函数:fix/round/floor/ceil
  • 原文地址:https://www.cnblogs.com/jugglee/p/8746286.html
Copyright © 2011-2022 走看看