因为一些配置信息,多处用到的。且以后可能变更的,我想写个.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);