数据驱动第三章节:使用Properties实现对属性文件的操作
1.Properties简介
Properties(Java.util.Properties),该类主要用于读取Java的配置文件,不同的编程语言有自己所支持的配置文件,配置文件中很多变量是经常改变的,为了方便用户的配置,能让用户够脱离程序本身去修改相关的变量设置,其配置文件常为.properties文件,以键值对的形式进行参数配置。
2.属性文件conf.properties的内容如下:
3.操作属性文件的类
package com.common;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* @Description:该类的功能描述
* @ClassName:PropertiesAction.java
* @version:v1.0.0
* @author:bingkele
* @date:2020年6月7日上午11:30:40
*/
public class PropertiesAction {
public static String profilePath = System.getProperty("user.dir")+"/conf.properties";
public static File profile = new File(profilePath);
public static Properties ps = new Properties();
/**
* @Description:获取指定的属性值
* @MethodName:getPropertiesValue
* @version:v1.0.0
* @author:bingkele
* @date:2020年6月7日下午15:51:11
*/
public static String getPropertiesValue(Properties pro,String key)throws Exception{
FileInputStream fs = new FileInputStream(profile);
InputStream in = new BufferedInputStream(fs);
ps.load(new InputStreamReader(in,"utf-8")); //将字节缓冲输入流转换为字符流,同时设置编码格式,不然会乱码
in.close();
return ps.getProperty(key);
}
/**
* @Description:设置指定的属性值,写入时以指定的键值对写入
* @MethodName:setProperties
* @version:v1.0.0
* @author:bingkele
* @date:2020年6月7日下午15:51:11
*/
public static void setProperties(Properties pro,String key,String value)throws Exception{
pro.setProperty(key, value); //设置属性,以键值对方式
//true表示追加打开(重写之前内容),false每次都是清空再重写(不会重写之前内容)
FileOutputStream fo = new FileOutputStream(profile,false);
pro.store(new OutputStreamWriter(fo,"utf-8"), null);
fo.close();
}
/**
* @Description:遍历所有属性值
* @MethodName:printInfo
* @version:v1.0.0
* @author:bingkele
* @date:2020年6月7日下午15:51:11
*/
public static void printInfo()throws Exception{
FileInputStream fs = new FileInputStream(profile);
InputStream in = new BufferedInputStream(fs);
Properties ps = new Properties();
ps.load(new InputStreamReader(in,"utf-8"));
//方法一:
Set<Object> keys = ps.keySet(); //返回属性key的Set集合
for(Object key:keys){
System.out.println("key:"+key.toString()+",value:"+ps.get(key));
}
// //方法二:
// Set<Entry<Object, Object>> entrys = ps.entrySet();//返回的属性键值对实体
// for(Entry<Object, Object> entry:entrys){
// System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());
// }
// //方法三:
// Enumeration<?> e = ps.propertyNames(); //返回枚举类型
// while (e.hasMoreElements()) {
// String key = (String) e.nextElement();
// String value = ps.getProperty(key);
// System.out.println("Key:" + key + ",Value:" + value);
// }
}
}
4.使用testNG进行测试
package com.testngdemo;
import org.testng.annotations.Test;
import com.common.PropertiesAction;
public class TestProperties {
@Test
public void test1() throws Exception{
String port = PropertiesAction.getPropertiesValue(PropertiesAction.ps,"端口");
String username = PropertiesAction.getPropertiesValue(PropertiesAction.ps,"username");
System.out.println("端口为:"+"-->"+port);
System.out.println("用户名为:"+"-->"+username);
}
@Test(enabled = false)
public void test2() throws Exception{
PropertiesAction.setProperties(PropertiesAction.ps,"username", "wang");
PropertiesAction.setProperties(PropertiesAction.ps,"passwod", "mima123456");
PropertiesAction.setProperties(PropertiesAction.ps,"uuid", "123456789");
PropertiesAction.setProperties(PropertiesAction.ps,"端口", "4723");
PropertiesAction.setProperties(PropertiesAction.ps,"IP地址", "127.0.0.1");
}
@Test
public void test3() throws Exception{
PropertiesAction.printInfo();
}
}
5.结果如下:
6.补充常用方法(仅供参考)