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

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

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

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

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

我把配置文件放在了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

相关文章:

  • 2021-11-07
  • 2021-09-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2021-08-18
猜你喜欢
  • 2022-12-23
  • 2021-10-06
  • 2021-10-27
  • 2021-09-04
  • 2022-12-23
相关资源
相似解决方案