Android 文件方式配置和读取(properties)方法

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:39   974   0

1.配置properties文件

先创建properties文件,规范存放在assets文件夹下,文件名随便起

新建完,文件结构是这样的

properties文件内容:  
格式:key=value
user=admin
password=123456

2 . 获取properties文件方法

public class PropertiesUtil {

    private static Properties properties;

    static {
        properties = new Properties();
        try {
//方法一:通过activity中的context攻取setting.properties的FileInputStream
        // 注意这地方的参数appConfig在eclipse中应该是appConfig.properties才对,但在studio中不用写后缀
          InputStream is = BaseApplication.getIns().getAssets().open("appConfig");
            //方法二:通过class获取setting.properties的FileInputStream
            //这种方法无效,可能是路径问题,大家有思路可以留言
            //InputStream is = PropertiesUtil.class.getResourceAsStream("/assets/appConfig.properties");
            properties.load(is);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getValue (String key){
        return properties.getProperty(key);
    }

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP