天天看點

java properties檔案_java解析properties檔案

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 {

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

}

}

繼續閱讀