java解析properties文件

Posted 经伟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    }

}

 

以上是关于java解析properties文件的主要内容,如果未能解决你的问题,请参考以下文章

java 怎么读取resource properties 并且解析成map

Java解析property文件(和静哥说的,SQL执行限定时间写在xml中,增加扩展,在不改源代码基础上)

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段

片段(Java) | 机试题+算法思路+考点+代码解析 2023

Java 配置解析Properties文件