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文件的更多相关文章
- 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分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- Java之properties文件读取
1.工程结构 2.ConfigFileTest.java package com.configfile; import java.io.IOException; import java.io.Inpu ...
- 用java读取properties文件--转
今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享. 下面直接贴出代码:java类 public class Mytest pub ...
- JAVA操作properties文件
va中的properties文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件,文件的内容是格式是"键=值"的格式,在properties ...
- java解析xml文件并输出
使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...
- java 读取properties文件总结
一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...
- java基础学习总结——java读取properties文件总结
摘录自:http://www.cnblogs.com/xdp-gacl/p/3640211.html 一.java读取properties文件总结 在java项目中,操作properties文件是经常 ...
- java读取properties文件时候要注意的地方
java读取properties文件时,一定要注意properties里面后面出现的空格! 比如:filepath = /home/cps/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
随机推荐
- delphi中的Label控件背景透明
Label1.Transparent:=true;你在它的属性窗口把它的Transparent属性改成TRUE就行了 来自为知笔记(Wiz)
- 浅析c语言中的变量(局部变量,外部变量,静态变量,寄存器变量)[转]
c语言中变量分为四类,分别是 1.auto 自动变量 2.static 静态存贮分配变量(又分为内部静态和外部静态) 3.extern 全程变量(用于外部变量说明) 4.register ...
- SQL SERVER ->> Data Compression
最近做了一个关于数据压缩的项目,要把整个SQL SERVER服务器下所有的表对象要改成页压缩.于是趁此机会了解了一下SQL SERVER下压缩技术. 这篇文章几乎就是完全指导手册了 https://t ...
- Error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToString(wchar_t *)"
Error 2 error LNK2019: unresolved external symbol "char * __stdcall _com_util::ConvertBSTRToStr ...
- SGU 132 Another Chocolate Maniac 状态压缩DP
感觉不是很好写的一道状态压缩. dp[i][j][k]表示第 i 行状态为k,第i - 1行状态为 j,具体细节见代码. 内存卡的很死,要用滚动数组. 还有一个比较坑爹的地方是它在输入蛋糕的时候中间可 ...
- cmd执行mssql脚本或者执行mysql脚本
private static int ExecuteMSSql(DbInfo db, string sqlPath) { Console.WriteLine("=============== ...
- php面向对象(二) $this
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- excel表格公式出现#REF是什么意思
#REF!错误是当单元格引用无效时,显示#REF错误 在出现下列情况时,发生此错误: (1)删除了公式引用的单元格.如A2中=A1-1,若A1被删除了,则显示此错误: (2)当被剪切的一个范围粘贴到一 ...
- Hadoop集群(第6期)_WordCount运行详解
1.MapReduce理论简介 1.1 MapReduce编程模型 MapReduce采用"分而治之"的思想,把对大规模数据集的操作,分发给一个主节点管理下的各个分节点共同完成,然 ...
- Mysql中的Prepared Statement与Stored Precedure学习
可以参考: http://stackoverflow.com/questions/196652/prepared-statement-vs-stored-procedure They are not ...