android 读.properties属性文件,按不同Key获取内容

本文介绍了如何在Java中打开指定路径的.prop.properties文件,通过`Properties`类读取并获取其中的属性值,如Wi-FiSSID、密码等,以及处理可能出现的FileNotFoundException和IOException。
摘要由CSDN通过智能技术生成

 以下是prop.properties属性文件内容,也是需要读的内容

wifi_ssid = office
wifi_pwd = test123456
user_code = 654
password = 123456
resource_code = MK_SMT_CS1MMI

不废话直接上代码

1、打开指定路径.properties文件

private static Properties mCurrentProperties = new Properties();
public static int startParser(Context context, String Path) {
    try {


        //2,创建流对象
        Path += "/prop.properties";
        File file = new File(Path);
        if (!file.exists()) {
            return -3;
        }
        FileInputStream in = new FileInputStream(Path);
        //FileReader in = new FileReader("prop.properties");
        //3,把流所对应文件中的数据 读取到集合中
        mCurrentProperties.load(in);
        //4,关闭流
        in.close();
        //5,显示集合中的数据
        return 0;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return -1;
    } catch (IOException e) {
        e.printStackTrace();
        return -2;
    }
}

2、获取属性值 返回String

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

调用例子:

Thread thread = new Thread(new Runnable() {
        @Override
    public void run() {
        String Path = getSecondaryStoragePath();//这里是我获取U盘路径的接口,可以使用自己的路径
        int ret = startParser(MES_TEST.this,Path);
        if(ret != 0){
            return;
        }
        String ssid = getValue("wifi_ssid");
        String pws = getValue("wifi_pwd");
        String UserCode = getValue("user_code");
        String PassWord = getValue("password");
        String ResCode = getValue("resource_code");
    }
});

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值