以下是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");
}
});