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/ 我找了半天,系统一直提示,没有这个路径,可是确实是存在的, ...
随机推荐
- Centos环境下部署游戏服务器-常用命令
图1 在Linux的世界,如果你不玩命令,那你见了同行都不好意思和人家打招呼.同时服务器正常状况下放在远端,一般都是开ssh登录服务器,相信远程桌面的人很少见吧.这篇文章说说Linu ...
- jQuery:balloon气泡提示插件
插件下载地址:http://file.urin.take-uma.net/jquery.balloon.js-Demo.html <!DOCTYPE html PUBLIC "-//W ...
- MAVEN ERROR: unable to find valid certification path to requested target 解决办法
第一次使用MAVEN编译项目,出现如下错误 解决办法:Maven的setting.xml中添加如下代码 <mirrors> <mirror> <id>Central ...
- Elsevier 投稿各种状态总结
Elsevier 投稿各种状态总结1. Submitted to Journal 当上传结束后,显示的状态是Submitted to Journal,这个状态是自然形成的无需处理.2. Wi ...
- URAL 1233 Amusing Numbers 好题
参照了nocow上的解法,照搬过来…… 易知一个数X在数列中在另一个数Y前,当且仅当X前缀小于Y或前缀相等X短,那么我们分布考虑,比如对于数48561: 5位上:10000~48560; 4位上:10 ...
- Data Base sqlServer基础知识
sqlServer 基础知识 大纲 创建数据库 1 创建表 2 备份表 3 删除表 4 修改表 5 查询出重复的数据 6 增删改查 7 添加约束 8 分页存储过程 9 排序 10 类型转换 11 ...
- Virtualbox: Shared directory- “unknown filesystem type vboxsf”
1. "设置”中,"共享文件夹”,把要共享的文件夹添加上. 2. 然后打开系统,找到“安装增强功能”,这时桌面上多了一个光盘或者看/media/下面是不是已经文件了,虽然这个时候可 ...
- iPhone 屏幕上的 Home 键在哪里?(已解决)
「问」:iPhone屏幕上的Home键如何开启? 「答」:在[设置]-[通用]-[辅助功能]-[AssistiveTouch],打开即可.不需要下载什么App. [Settings] - [Gener ...
- armeabi,armeabi-v7a ,x86 和mips 都是什么?
首先要明白ABI的概念: ABI(Application Binary Interface)实际就是指应用程序基于哪种指令集来进行编译,我们能用到的ABI 也就四种 armeabi,armeabi ...
- 加密解密(5)SSL形象比喻
转自: http://blog.csdn.net/cloverphp/article/details/11737433 前言: 关于公钥,私钥请看前几篇文章 SSL 协议既用到了公钥加密技术(握手 ...