android读取properties配置文件

来源:互联网 发布:linux开机动画 编辑:程序博客网 时间:2024/05/18 05:04

转载来源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);  

0 0
原创粉丝点击