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/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
随机推荐
- 修改webapp底图
从webapp目录可以看出地图归mapManager处理,所以在MapManager.js中找关于加载地图的方法, 很容易在里面找到showMap方法: 下面有另一个方法_showMap方法,查看定义 ...
- Find K most Frequent items in array
给定一个String数组,求K个出现最频繁的数. 记录一下查到的资料和思路: 1. 使用heap sorting, 先用hashmap求出单词和词频.需要额外建立一个class Node,把单词和词频 ...
- MS WORD 表格自动调整列宽,自动变漂亮,根据内容自动调整 .
在MS WORD中,当有大量的表格出现时,调整每个表格的的高和宽和大小将是一件非常累的事情,拖来拖去,非常耗时间,而且当WORD文档达到300页以上时,调整反应非常的慢,每次拖拉线后,需要等待一段时间 ...
- SSIS ->> Script Debugging and Troubleshooting
Breakpoint是调试过程中最重要的手段,不仅对于Script Task和Script Component,对于任何其他的组件也是如此.可以在某个Event(如OnError)触发的时候设置断点来 ...
- 【分享】IT产业中的三大定理(一) —— 摩尔定理(Moore's Law)
科技行业流传着很多关于比尔·盖茨的故事,其中一个是他和通用汽车公司老板之间的对话.盖茨说,如果汽车工业能够像计算机领域一样发展,那么今天,买一辆汽车只需要 25 美元,一升汽油能跑四百公里.通用汽车老 ...
- poj -2010 Moo University - Financial Aid (优先队列)
http://poj.org/problem?id=2010 "Moo U"大学有一种非常严格的入学考试(CSAT) ,每头小牛都会有一个得分.然而,"Moo U&quo ...
- .htaccess的应用
今天下班后,同事问到这个东西的用法.我以前也没接触过,特地搜索了一下,现在保存方便以后使用. 1.首先判断服务器类型,服务器是否支持. 2.写法是否有错误. 3.需要的功能是否在.htaccess是否 ...
- leetcode:Reverse Linked List
Reverse a singly linked list. 代码如下: the iterative solution:(c++) /** * Definition for singly-linked ...
- Hadoop集群(第10期)_MapReduce与MySQL交互
2.MapReduce与MySQL交互 MapReduce技术推出后,曾遭到关系数据库研究者的挑剔和批评,认为MapReduce不具备有类似于关系数据库中的结构化数据存储和处理能力.为此,Google ...
- 聚集索引、非聚集索引、聚集索引组织表、堆组织表、Mysql/PostgreSQL对比、联合主键/自增长、InnoDB/MyISAM(引擎方面另开一篇)
参考了多篇文章,分别记录,如下. 下面是第一篇的总结 http://www.jb51.net/article/76007.htm: 在MySQL中,InnoDB引擎表是(聚集)索引组织表(cluste ...