喜欢研究学习技术,喜欢和志同道合的人交流。 从事测试6年,专职性能3年经验,擅长性能测试,测试框架开发。 励志格言:只要想学习,永远都不会太晚;只要想进步,永远都会有空间。

java从properties配置文件读取配置内容

上一篇 / 下一篇  2016-06-02 11:49:12 / 个人分类:selenium自动化测试

java从properties配置文件读取配置内容,通过Key值,获取到对应的value值读法。

1、properties配置文件内容如下:

name=xiaoming
passWord=123456
add=hunan
email=1198874995@QQ.com

2、去读properties文件方法
  传入参数为,properties文件读取的路径,以及key值,传入的key值需要判断properties配置文件中是否存在,
  如果key值存在则返回value值的内容,如果不存在提示。
public class BasePropertiesFile {

public String readPropertiesFile(String propertiesFilePath, String key) {
Properties properties = new Properties();
FileInputStream fileInputStream = null;

try {
fileInputStream = new FileInputStream(propertiesFilePath);
properties.load(fileInputStream);
if (properties.containsKey(key)) {
return properties.getProperty(key);
} else {
System.out.println("传入的Key值不存在!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) { 
fileInputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return properties.getProperty(key);
}
}

3、测试是是否读取成功。
public class TestTest {

public static void main(String[] args) throws FileNotFoundException {
String filePath = "C:\\Users\\Administrator\\workspace\\20160413\\files\\test.properties";
System.out.println("name的值为:"+new BasePropertiesFile().readPropertiesFile(filePath, "name"));
}

}

输出结果:name的值为:xiaoming






TAG: java 配置文件

 

评分:0

我来说两句

Open Toolbar