读取配置文件的方法
2016-01-06 09:55
218 查看
public static List<String> hosts = new ArrayList<String>(); static Properties pros= new Properties(); static{ try { pros.load(Content.class.getResourceAsStream("/main_config.properties")); isDebug = Boolean.valueOf(pros.getProperty("debug")); if(!isDebug){ host=pros.getProperty("host"); //3d加密key triple_des_key=pros.getProperty("triple_des_key"); //短信 sms_ip=pros.getProperty("sms_ip"); sms_port=Integer.valueOf(pros.getProperty("sms_port")); isAbsolute = Boolean.valueOf(pros.getProperty("isAbsolute")); hosts = initClusters(); //图片 article_img_path=pros.getProperty("article_img_path"); coupon_img_path=pros.getProperty("coupon_img_path"); couponold_img_path=pros.getProperty("couponold_img_path"); slide_img_path=pros.getProperty("slide_img_path"); //路径 filePath=pros.getProperty("filePath"); couponold_filePath=pros.getProperty("couponold_filePath"); coupon_filePath=pros.getProperty("coupon_filePath"); //阿里二维码支付 alipay_partner = pros.getProperty("alipay_partner"); alipay_key = pros.getProperty("alipay_key"); alipay_seller_email = pros.getProperty("alipay_seller_email"); }else{ host=pros.getProperty("debug_host"); triple_des_key=pros.getProperty("debug_triple_des_key"); //短信 sms_ip=pros.getProperty("debug_sms_ip"); sms_port=Integer.valueOf(pros.getProperty("debug_sms_port")); isAbsolute = Boolean.valueOf(pros.getProperty("debug_isAbsolute")); hosts = debug_initClusters(); //图片 article_img_path=pros.getProperty("debug_article_img_path"); coupon_img_path=pros.getProperty("debug_coupon_img_path"); couponold_img_path=pros.getProperty("debug_couponold_img_path"); slide_img_path=pros.getProperty("debug_slide_img_path"); //路径 filePath=pros.getProperty("debug_filePath"); couponold_filePath=pros.getProperty("debug_couponold_filePath"); coupon_filePath=pros.getProperty("debug_coupon_filePath"); alipay_partner = pros.getProperty("debug_alipay_partner"); alipay_key = pros.getProperty("debug_alipay_key"); alipay_seller_email = pros.getProperty("debug_alipay_seller_email"); } } catch (IOException e) { e.printStackTrace(); } }
总结:
首先把名为main_config.properties的配置文件放在src下即可,配置文件里的数据样式为name=value。
例如:isAbsolute=true
sms_port=8090
在项目当前类中引入Properties类,Properties pros = new Properties();
pros.load(当前类.class.getResourseAsStream("/main_config.properties"));
当然有时候我们需要在项目上线之前先使用测试环境的配置文件。这时可以巧妙地在配置文件中定义一个变量(变量名字随便起)如 isDebug ,让其值为true或者false,当值为true时,可以加载测试的配置数据,为false时加载正式的数据。其作用相当于一个开关。
紧接着就可以随心所欲的获取配置文件里的数据值了。如: String sms_port = pros.getProperty("sms_port");
..........................................
然后就没有然后了,大功告成!
相关文章推荐
- 80端口被NT kernel & System 占用pid 4
- canvas的save和restore
- 网络连接
- java小结之练习
- 如何屏蔽Button setClickable与setEnabled
- 初始化openwrt的rootpassword
- JAVA多线程售票问题
- 网页返回码大全
- ReactiveCocoa v2.5 源码解析 之 架构总览
- JpGraph使用详解之中文乱码解决方法
- JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html
- Android开发之imageView图片按比例缩放的实现方法
- js实现仿qq消息的弹出窗效果
- 深入浅出数据分析
- 远程推送
- 记一次ganglia的故障分析 mem_report不显示
- 创建自定义的控件
- WP-Polls投票汉化插件(bug修改)
- 2016年最热门的15 款代码语法高亮工具,美化你的代码
- Reduce统计结果汇总混乱