Java学习-020-Properties 判断是否存在对应的 key 项
在日常的脚本编写过程中,通常会判断配置文件中是否存在对应的配置项,以判断是否执行相应的业务逻辑。
小二上码。。。若有不足之处,敬请大神指正,不胜感激!
判断是否存在 key 项(配置项)的方法源码如下所示:
/**
* Verify the key contains in properties or not
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesKeyIsExist, 2014-11-20 16:31:10 Exp $
*
* @param prop : properties
* @param key : key
* @return boolean
*/
public boolean propertiesKeyIsExist(Properties prop, String key){
boolean success = false;
String item = ""; // verify the properties file is null
if (prop == null) {
this.message = "The content of properties file is null !";
this.logger.error(this.message); success = false;
return success;
} // verify the key is null
if ("".equals(key) || key == null) {
this.message = "There is no key {" + key + "} in properties config file.";
this.logger.error(this.message); success = false;
return success;
} // get keys from properties
Enumeration<?> enu = prop.propertyNames(); // verify the key is contains in properties or not
while (enu.hasMoreElements()) {
item = (String)enu.nextElement(); if (item.equals(key)) {
success = true;
}
} return success;
}
判断 properties 配置文件是否存在相应的配置项源码
测试源码如下所示:
/**
* Test : Verify the key contains in properties file or not
*
* @author Aaron.ffp
* @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesKeyIsExist, 2014-11-20 16:35:15 Exp $
*
*/
public void test_propertiesKeyIsExist(){
this.message = "\n\n\nTEST:FileUtils.propertiesKeyIsExist(Properties prop, String key)";
this.logger.debug(this.message); this.fu = new FileUtils();
String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR +
"testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties"; Properties prop = this.fu.propertiesRead(filename); // print-1
prop.list(System.out); System.out.println("\n\n"); Assert.assertEquals(this.fu.propertiesKeyIsExist(prop, "host"), true, "Test case failed.");
}
测试源代码
至此, Java学习-020-Properties 判断是否存在对应的 key 项 顺利完结,希望此文能够给初学 Java 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^
Java学习-020-Properties 判断是否存在对应的 key 项的更多相关文章
- Java学习-030-JSON 之四 -- 判断 JSONObject 是否包含键值对
前文对获取 JSON 数据封装方法,使之可通过类似于 cssSelector 的方法获取 JSON 数据,使获取数据变得简单.敬请参阅:模仿 cssSelector 封装读取 JSON 数据方法. 在 ...
- Java学习:Properties类
Java学习:Properties类 学习目标 认识properties文件,理解其含义,会正确创建properties文件. 会使用java.util.Properties类来操作propertie ...
- Java学习之==>条件判断、循环控制
一.条件判断 1.if-else 示例: /** * 第1种,1个分支 */ public void case1() { int age = 15; if (age > 18) { System ...
- Java学习笔记31(IO:Properties类)
Properties类,表示一个持久的j集,可以存在流中,或者从流中加载 是Hashtable的子类 map集合的方法都能用 用途之一:在开发项目中,我们最后交给客户的是一个编译过的class文件,客 ...
- Java学习笔记(二)——Java操作properties文件
[前面的话] 前段时间在学习和玩java web相关的东西,对于这些技术,一边学习,一边做东西,一边总结,希望可以一边成长和有所收获.有时总是思考太多反而成为了前进的阻力,所以对于生活还是简单一些,不 ...
- Java学习笔记——JDBC读取properties属性文件
Java 中的 properties 文件是一种配置文件,主要用于表达配置信息,文件类型为*.properties,格式为文本文件. 文件的内容是格式是"键=值"(key-valu ...
- 20145330第十周《Java学习笔记》
20145330第十周<Java学习笔记> 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就 ...
- Java学习-022-Properties 文件数据写入
Properties 配置文件写入主要通过 Properties.setProperty 和 Properties.store 两个方法,此文以一个简单的 properties 文件写入源码做示例. ...
- Java学习-008-判断文件类型实例
此文源码主要为应用 Java 如何判断文件类型的源码及其测试源码.若有不足之处,敬请大神指正,不胜感激!源代码测试通过日期为:2015-2-2 23:02:00,请知悉. Java 判断文件类型源码如 ...
随机推荐
- 20145315 《Java程序设计》实验五实验报告
20145315 <Java程序设计>实验五实验报告 实验五 Java网络编程 我负责服务器部分,王嘉澜负责客户端部分:http://www.cnblogs.com/SJZGM10/p/5 ...
- MONO 架构
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
- jQuery 写的插件图片上下切换幻灯效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 使用本地yum源
[root@xaiofan ~]# mount /dev/sr0 /mnt/ [root@xaiofan ~]# cat /etc/yum.repos.d/local.repo [loacl]name ...
- web app
*********Modernizr作为开发HTML5必要的js工具************ Modernizr作为开发HTML5必要的js工具 *********字体大小设 rem********* ...
- java---一元二次方程练习
public class wu{ public static void main(String[] args){ int a = 2,b = 1, c = 0,d = b*b-4*a*c if (a ...
- unity 环境增强
早上过来解决两个问题: (1)VS2012 配置下 VA_X 插件,现在VS在字体颜色方面已经提高很多,但是在类成员实时显示方面还是不方便,或者我没找到吧 (2)安装unityVs插件,微软把 ...
- RT-Thread 线程调度
/* 变量分配4字节对齐 */ ALIGN(RT_ALIGN_SIZE) /* 静态线程的 线程堆栈*/ ]; ]; /* 静态线程的 线程控制块 */ static struct rt_thread ...
- Why Apache Beam? A data Artisans perspective
https://cloud.google.com/dataflow/blog/dataflow-beam-and-spark-comparison https://github.com/apache/ ...