因為一些配置信息,多處用到的。且以後可能變更的,我想寫個.prorperties配置文件給管理起來。
我把配置文件放在瞭assets文件夾下
appConfig.properties:
serverUrl=https://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);