在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择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文件的更多相关文章

  1. 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 这两个注解应用在 ...

  2. java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)

     java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...

  3. Java之properties文件读取

    1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...

  4. 用java读取properties文件--转

    今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest pub ...

  5. JAVA操作properties文件

    va中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties ...

  6. java解析xml文件并输出

    使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...

  7. java 读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  8. java基础学习总结——java读取properties文件总结

    摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...

  9. java读取properties文件时候要注意的地方

    java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...

随机推荐

  1. Android笔记——Handler更新UI示例

    public class MainActivity extends ActionBarActivity { private TextView textView; private int i=0; @O ...

  2. oracle记录解锁

    oracle 怎样查一个表中的记录是否被锁住了   怎么查询一个数据库中有几个表引用了其中某个特定表的主键做为其外键的select t.table_name from user_constraints ...

  3. Ext.grid.plugin.RowExpander的简单用法

    有时候,我们在grid里渲染数据时,由于某些字段的内容太长,而grid又不会自动出现滚动条,于是溢出的内容后面就会出现省略号, 导致信息展示不完全.如果,这个信息不太重要,展示不完全也无关紧要.可是, ...

  4. BZOJ 2751 容易题

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2751 题意:有一个数列A已知对于所有的A[i]都是1到n的自然数,并且知道对于一些A[i ...

  5. apache for mac OX S 10.10

    mac下如何针对 apache 设置虚拟目录呢?可能很多人都设置过,但也都不太会,每次都是网上找文章啥的.这里,我自己整理了一点,希望可以帮到大家. 还原 httpd.conf 配置文件 如果,你现在 ...

  6. iOS-APP中添加启动界面

    iOS,非常高兴的又回到了这个领域.纠结的就业之路...找工作的这段时间里,原本以为是iOS的游戏开发,却一直只有后台的offer,到Android,最后正式上班的时候却被告知调到了iOS开发,当然不 ...

  7. 如何在linux中从源代码编译安装nodejs?

    $ sudo yum groupinstall 'Development Tools'安装开发环境$ wget https://nodejs.org/dist/v0.12.2/node-v0.12.2 ...

  8. core--线程池

    对于服务器-客户端这种架构的软件,通常客户端的数据来自于服务器,如何让一个服务器进程,来满足多个客户端程序的数据请求?一种简单的方法就是,每当一个客户请求来领,服务器就为该客户端创建一个线程.当有10 ...

  9. Spring 实践 -AOP

    Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...

  10. LA 3695 Distant Galaxy

    给出n个点的坐标(坐标均为正数),求最多有多少点能同在一个矩形的边界上. 题解里是构造了这样的几个数组,图中表示的很明白了. 首先枚举两条水平线,然后left[i]表示竖线i左边位于水平线上的点,on ...