java.util.Properties 读取配置文件中的参数

 

用法

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

   java.util.Properties 读取配置文件中的参数   

//读取配置文件 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(); } } }

 

posted @ 2019-11-28 18:21  桂长江  阅读(544)  评论(0编辑  收藏  举报