java解析properties文件

时间:2023-03-09 17:17:39
java解析properties文件

在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理properties文件,虽然名字叫properties,其实打开它发现就是一个记事本的文件,所以看起来也比较直观,下面是解析properties文件的实现代码。

properties文件里存贮的样子是这样的,然后给他保存为xxx.properties即可。

gsBAMUserName1=automation_bam_dg1
gsBAMUserName1_FirstName=DaGuang_BAM
gsBAMUserName1_LastName=Zhu

代码:

package utilities;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
@SuppressWarnings("unused")
public class PropertiesOperation { /**
* Script Name : <b>PropertiesOperation</b>
* Generated : <b>May 21, 2012 2:03:25 PM</b>
* Description : Functional Test Script
* Original Host : WinNT Version 5.1 Build 2600 (S)
*/
public static String filename1= "supportfiles/PropertyFiles/Sourcing.properties";
public static String filename2= "supportfiles/PropertyFiles/TestCaseMapping.properties";
public static String filename3 = "supportfiles/PropertyFiles/RFxNumber.properties";
public static String filename4 = "supportfiles/PropertyFiles/RFxNumber.properties";
public static Properties properties;
public static FileInputStream inputFile;
public static FileOutputStream outputFile; //获取properties文件里值得主要方法,根据它的key来获取
public static String getSourcingValueBykey(String key){
String value="";
try{ FileInputStream inputFile = new FileInputStream(filename1);
Properties properties = new Properties();
properties.load(inputFile);
inputFile.close(); value = properties.getProperty(key);
if(value == null || value.equals("")){
System.out.println("The value for key: " + key + " doesn't exist.");
System.out.println("Please check the content of the properties file."); }
}catch(Exception e){
e.printStackTrace();
}
return value;
} //Added by Justin 06/13
//Store the new key/value pair to the property file and save the file
public static void setRFxNumberBykey(String key, String value){ String description = "Property file for Sourcing Automation";
//filename4 = getRFxNumber(); Properties prop = new Properties();
try{ FileInputStream fis = new FileInputStream(filename4);
prop.load(fis);
fis.close(); prop.setProperty(key, value);
FileOutputStream fos = new FileOutputStream(filename4); prop.store(fos, description);
fos.close();
}catch(Exception e){
e.printStackTrace();
} } //Read the RFxNumber property file to get the RFx or Item number saved during execution
public static String getRFxNumberValueBykey(String key){
String value="";
//filename4 = getRFxNumber();
try{ FileInputStream inputFile = new FileInputStream(filename4);
Properties properties = new Properties();
properties.load(inputFile);
inputFile.close(); value = properties.getProperty(key);
if(value == null || value.equals("")){
System.out.println("The value for key: " + key + " doesn't exist.");
System.out.println("Please check the content of the properties file.");
}
}catch(Exception e){
e.printStackTrace();
}
return value;
} //Read the TestCaseMapping property file to get the Spreadsheet excel filename
public static String getTestCaseMappingBykey(String key){
String value="";
try{
FileInputStream in = new FileInputStream(filename2);
Properties settings = new Properties();
settings.load(in);
value = settings.getProperty(key);
if(value==null||value.equals("")){
System.out.println("The value for key: " + key + " doesn't exist.");
System.out.println("Please check the content of the properties file.");
}
}catch(Exception e){
e.getMessage();
}
return value;
}
// public static String getRFxNumber(){
// filename4= getSourcingValueBykey("gsAutoDataPath")+"RFxNumber.Properties";
//
// System.out.println("filename4:" + filename4);
// return filename4;
// } public static void main(String[] args) throws IOException {
//getRFxNumber();
} }