android读取properties配置文件
16lz
2022-06-05
转载来源http://blog.csdn.net/howlaa/article/details/18305289?utm_source=tuicool&utm_medium=referral
因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。
我把配置文件放在了assets文件夹下
appConfig.properties:
[html] view plain copy
- serverUrl=http://192.168.1.155/ap
操作的工具类:
MyProperUtil.Java:
[java] view plain copy
- 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;
- }
- }
使用:
[java] view plain copy
- properties = MyProperUtil.getProperties(getApplicationContext());
- url = properties.getProperty("serverUrl");
- Log.i("URL", url);
更多相关文章
- Android(安卓)解决setRequestedOrientation之后手机屏幕的旋转不
- Android开发中出现Attempt to invoke virtual method...on a nul
- android 四大组件之Service
- Android(安卓)Intents
- Android(安卓)Framework 之 SystemUI的启动流程
- Android(安卓)UI测量、布局、绘制过程探究
- 【Android】adapter has changed解决方法
- 浅谈Java中Collections.sort对List排序的两种方法
- Python list sort方法的具体使用