首页 > 其他 > 详细

读取配置文件

时间:2018-06-14 12:48:27      阅读:206      评论:0      收藏:0      [点我收藏+]
 
第一种方式
jar 
技术分享图片
public class cshi {
     public static void main(String[] args) {
           System.out.println(getProperty("A1"));
     }
     private static PropertiesConfiguration config;
     static {
           try {
                config = new PropertiesConfiguration();
                config.setEncoding("UTF-8"); 先设置编码在加载文件
                config.load("sss.properties");
           } catch (ConfigurationException e) {
                e.printStackTrace();
           }
           // 设置reload策略,这里用当文件被修改之后reload(默认5s中检测一次)
           FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
           strategy.setRefreshDelay(20000);
           config.setReloadingStrategy(strategy);
     }
     public static synchronized String getProperty(String key) {
           return (String) config.getProperty(key);
     }
}
 
第二种方式:
public static String readProperties(String key) {
           // 读取信息文件
           ClassPathResource cr = new ClassPathResource("pay.properties");// 会重新加载spring框架
           Properties pros = new Properties();
           try {
                pros.load(cr.getInputStream());
           } catch (IOException ex) {
                System.out.println("读取信息文件错误");
           }
           String url = pros.getProperty(key);
           return url;
     }

读取配置文件

原文:https://www.cnblogs.com/bchange/p/9181986.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!