java properties文件_java解析properties文件

该博客详细展示了如何使用Java进行Properties文件的操作,包括读取特定键的值、添加新的键值对并保存到文件、读取RFx编号属性文件以及获取测试用例映射的Excel文件名。通过示例代码解释了关键方法的实现过程。
摘要由CSDN通过智能技术生成

packageutilities;importjava.io.FileInputStream;importjava.io.BufferedInputStream;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.OutputStream;importjava.util.Enumeration;importjava.util.Properties;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStream;importjava.util.Properties;

@SuppressWarnings("unused")public classPropertiesOperation {/*** Script Name : PropertiesOperation

* Generated : May 21, 2012 2:03:25 PM

* 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 staticProperties properties;public staticFileInputStream inputFile;public staticFileOutputStream outputFile;

//获取properties文件里值得主要方法,根据它的key来获取public staticString getSourcingValueBykey(String key){

String value="";try{

FileInputStream inputFile= newFileInputStream(filename1);

Properties properties= newProperties();

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();

}returnvalue;

}//Added by Justin 06/13//Store the new key/value pair to the property file and save the file

public static voidsetRFxNumberBykey(String key, String value){

String description= "Property file for Sourcing Automation";//filename4 = getRFxNumber();

Properties prop= newProperties();try{

FileInputStream fis= newFileInputStream(filename4);

prop.load(fis);

fis.close();

prop.setProperty(key, value);

FileOutputStream fos= newFileOutputStream(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 staticString getRFxNumberValueBykey(String key){

String value="";//filename4 = getRFxNumber();

try{

FileInputStream inputFile= newFileInputStream(filename4);

Properties properties= newProperties();

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();

}returnvalue;

}//Read the TestCaseMapping property file to get the Spreadsheet excel filename

public staticString getTestCaseMappingBykey(String key){

String value="";try{

FileInputStream in= newFileInputStream(filename2);

Properties settings= newProperties();

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();

}returnvalue;

}//public static String getRFxNumber(){//filename4= getSourcingValueBykey("gsAutoDataPath")+"RFxNumber.Properties";//

//System.out.println("filename4:" + filename4);//return filename4;//}

public static void main(String[] args) throwsIOException {//getRFxNumber();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值