android读取properties配置文件

因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。

我把配置文件放在了assets文件夹下

appConfig.properties:

serverUrl=http://192.168.1.155/ap

操作的工具类:

MyProperUtil.java:

package cn.com.smartcost.offer.util;

import java.io.InputStream;
import java.util.Properties;

import android.content.Context;

/**
 * 读取properties配置文件
 * 
 * @date 2014-1-15 10:06:38
 * 
 *
 */
public class MyProperUtil {
	private static Properties urlProps;
     public static Properties getProperties(Context c){
			Properties props = new Properties();
			try {
			//方法一:通过activity中的context攻取setting.properties的FileInputStream
			InputStream in = c.getAssets().open("appConfig.properties");
			//方法二:通过class获取setting.properties的FileInputStream
			//InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/  setting.properties ")); 
			props.load(in);
			} catch (Exception e1) {
			// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			urlProps = props;
			
				System.out.println(urlProps.getProperty("serverUrl"));
			      return urlProps;
			}

}

使用:

properties = MyProperUtil.getProperties(getApplicationContext());
		url = properties.getProperty("serverUrl");
		Log.i("URL", url);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值