Configuration properties file in Android project

佐手、 提交于 2019-12-12 03:19:55

问题


Where can I place configuration file in Android project? Currently I am made a directory in res directory as : res/config/configuration.properties

and want to access it as :
properties = new Properties();
properties.load(getClass().getResourceAsStream("config/configuration.properties"));

It is not working : input stream is null


回答1:


You can place it in assets directory as assets/configuration.properties

Example code

try {
    InputStream is = context.getAssets().open("configuration.properties");
    Properties props = new Properties();
    props.load(is);

    String value = props.getProperty("key", "");

    is.close();
} catch (Exception e) {
}



回答2:


Properties properties = new Properties();
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("config.properties");
properties.load(inputStream);
properties.getProperty(key);


来源:https://stackoverflow.com/questions/35075619/configuration-properties-file-in-android-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!