android读取properties配置文件

转载来源http://blog.csdn.net/howlaa/article/details/18305289?utm_source=tuicool&utm_medium=referral

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

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

appConfig.properties:

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. serverUrl=http://192.168.1.155/ap  

操作的工具类:

MyProperUtil.Java:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package cn.com.smartcost.offer.util;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.Properties;  
  5.   
  6. import android.content.Context;  
  7.   
  8. /** 
  9.  * 读取properties配置文件 
  10.  *  
  11.  * @date 2014-1-15 10:06:38 
  12.  *  
  13.  * 
  14.  */  
  15. public class MyProperUtil {  
  16.     private static Properties urlProps;  
  17.      public static Properties getProperties(Context c){  
  18.             Properties props = new Properties();  
  19.             try {  
  20.             //方法一:通过activity中的context攻取setting.properties的FileInputStream  
  21.             InputStream in = c.getAssets().open("appConfig.properties");  
  22.             //方法二:通过class获取setting.properties的FileInputStream  
  23.             //InputStream in = PropertiesUtill.class.getResourceAsStream("/assets/  setting.properties "));   
  24.             props.load(in);  
  25.             } catch (Exception e1) {  
  26.             // TODO Auto-generated catch block  
  27.                 e1.printStackTrace();  
  28.             }  
  29.               
  30.             urlProps = props;  
  31.               
  32.                 System.out.println(urlProps.getProperty("serverUrl"));  
  33.                   return urlProps;  
  34.             }  
  35.   
  36. }  

使用:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. properties = MyProperUtil.getProperties(getApplicationContext());  
  2.         url = properties.getProperty("serverUrl");  
  3.         Log.i("URL", url);  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值