因为一些配置信息,多处用到的。且以后可能变更的,我想写个.prorperties配置文件给管理起来。在studio中新建一个Assets文件——>新建一个file文件类型为properties文件,该文件可以与res,java同级

android 如何创建配置文件和读配置文件_配置信息

android 如何创建配置文件和读配置文件_properties文件_02

android 如何创建配置文件和读配置文件_配置信息_03

android 如何创建配置文件和读配置文件_java_04

android 如何创建配置文件和读配置文件_Android_05

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

appConfig:

serverUrl=http://192.168.110:8088/ap


操作的工具类:

ProperTies :

public class ProperTies {

public static Properties getProperties(Context c){
Properties urlProps;
Properties props = new Properties();
try {
//方法一:通过activity中的context攻取setting.properties的FileInputStream
//注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
//InputStream in = c.getAssets().open("appConfig.properties");
InputStream in = c.getAssets().open("appConfig");
//方法二:通过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;
return urlProps;
}
}


使用:

Properties proper = ProperTies.getProperties(getApplicationContext());
String serviceUrl = proper.getProperty("serverUrl");
Log.i("TAG", "serviceUrl=" + serviceUrl);


测试结果为:

01-25 07:51:50.743 13019-13019/? I/TAG: serviceUrl=http://192.168.110:8088/ap