喜欢研究学习技术,喜欢和志同道合的人交流。
从事测试6年,专职性能3年经验,擅长性能测试,测试框架开发。
励志格言:只要想学习,永远都不会太晚;只要想进步,永远都会有空间。
java从properties配置文件写入配置内容
上一篇 /
下一篇 2016-06-02 11:58:27
/ 个人分类:selenium自动化测试
1、通过
java实现向properties配置文件中,写入固定的配置信息内容。
参数为指定写入properties的路径,以及写入的key值和vaule值;写入文件输出流,需要加true参数,指定追加 内容。
public class BasePropertiesFile {
public void writerPropertiesFile(String propertiesFilePath,String key,String values) {
Properties properties = new Properties();
FileWriter fileWrite = null;
try {
fileWrite = new FileWriter(propertiesFilePath,true);
properties.setProperty(key,values);
properties.store(fileWrite, "write info");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWrite != null) {
fileWrite.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
}
2、
测试是否写入指定内容到指定的properties文件中。
public class TestTest {
public static void main(String[] args) throws FileNotFoundException {
String filePath = "C:\\Users\\Administrator\\workspace\\20160413\\files\\test.properties";
BasePropertiesFile tBasePropertiesFile=new BasePropertiesFile();
tBasePropertiesFile.writerPropertiesFile(filePath, "test", "123456");
}
}
显示写入的Properties配置文件内容:
#write info
#Thu Jun 02 12:03:17 CST 2016
test=123456
注意:在写入Properties文件内容的时候,可能存在多种的数据类型,但是Properties文件指定的只能传入String数据类型。
收藏
举报
TAG: