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

}
时间: 2024-08-10 02:10:02

java解析properties文件的相关文章

Spring课程 Spring入门篇 4-6 Spring bean装配之基于java的容器注解说明--@ImportResource和@Value java与properties文件交互

1 解析 1.1 这两个注解应用在什么地方 1.2 应用方式 1.3 xml方式实现取值 2 代码演练 2.1 @ImportResource和@Value代码演练 1 解析 1.1 这两个注解应用在什么地方 接口调用,java与properties文件交互获取url和用户名密码等配置信息 1.2 应用方式 java类通过调用@Importresource找到xml,通过xml配置properties 示例: @Configuration @ImportResource("classpath:c

Java获取.properties文件

@SuppressWarnings("rawtypes") public static void getProperties() { Properties properties = null; InputStream in = null; try { properties = new Properties(); //获取文件 in = Object.class.getResourceAsStream("/config.properties"); properties

JAVA操作properties文件

java中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties文件中,可以用"#"来作注释,properties文件在Java编程中用到的地方很多,操作很方便.一.properties文件test.properties------------------------------------------------------###########

Java读取Properties文件的六种方法

使用J2SE API读取Properties文件的六种方法 1.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2.使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb

java解析dbf文件

dbf文件, 不能直接另存为excel文件, 会丢数据! 需要将dbf数据存储到数据库中, 在从数据库中将数据导出成excel. import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.util.ArrayList; import java.u

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

Java 解析chm文件实战(原创)

需求:java解析chm文件,并将内容插入数据库和redis. Java解析chm文件,网上除了github上有个家伙只言片语了一下,没有啥资料参考,包括chm4j这东西,没啥介绍,本着服务大众的精神,整理了下流程, 时间仓促,错误之处在所难免,望指正. 第一步:下载chm4j.jar以及依赖 http://sourceforge.net/projects/chm4j/ 第二步:新建java工程,建一个解析ParseChm类,建一个解析测试类,类似: ParseChm类: //下面的包,请导入c

Java读取.properties文件

例1: 创建一个config文件夹 config文件夹中有一个Properties.properties文件 内容为: capitalLetter=ABCDE smallLetter=abcde 注意:config文件夹与包含Test类的包为同一级 import java.io.IOException; import java.util.Properties; public class Test { public static void main(String[] args) { Propert

Java读properties文件中文乱码问题的解决方法

java读properties文件,包含中文字符的主要有两种: 1.key中包含中文字符的(value中也有可能包含) 2.key中不包含中文字符的(value中有可能包含) 1.key中包含中文字符 可以使用java自带工具native2ascii.exe(Java\jdk1.x.x\bin\native2ascii.exe),转换文件编码格式 示例: native2ascii -encoding 8859_1 c:\a.properties c:\b.properties 即将 c:\a.p