读取配置文件的方法

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");

..........................................

然后就没有然后了,大功告成!







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值