读取配置文件的方法
来源:互联网 发布:linux格式化分区命令 编辑:程序博客网 时间:2024/05/29 10:07
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加密keytriple_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");
..........................................
然后就没有然后了,大功告成!
0 0
- 读取配置文件的方法
- 读取配置文件的方法案例
- 读取xml配置文件的方法
- 读取配置文件的方法getcfg()
- play读取配置文件的方法
- OSGI读取配置文件的方法
- spring 读取配置文件的方法
- spring 读取配置文件的方法
- 读取配置文件properties的方法
- play读取配置文件的方法
- Spring读取配置文件的方法
- java读取配置文件的方法
- 读取配置文件properties的方法
- 配置文件读取的n种方法
- java读取配置文件的几种方法
- java应用程序读取配置文件的方法
- java读取配置文件的几种方法
- java读取配置文件的几种方法
- 80端口被NT kernel & System 占用pid 4
- Linux下安装Matlab
- error while loading shared libraries: libg2c.so.0: cannot open shared object file
- canvas的save和restore
- Python学习手册 - 04
- 读取配置文件的方法
- 当总统
- 网络连接
- 汇编相关基本指令及初步学习网站
- C::B的自动换行问题
- 北斗在轨卫星
- Spring 配置多数据源实现数据库读写分离
- Windows media encoder 搭建流媒体服务器
- update mysql