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 判断文件类型源码如 ...
随机推荐
- Android 第三方
把优酷.土豆等取出它们真是的视频播放地址:http://vparser.com/ volley 项目地址 https://github.com/smanikandan14/Volley-demo (1 ...
- [Cocos2d-x For WP8]MotionStreak拖尾效果
拖尾效果是指在在游戏中,一个精灵在运动的过程中会留下一个短暂的轨迹效果,在游戏里面如打斗的特效往往会需要用到这种效果来给运动的增加绚丽的效果.那么在Cocos2D-x里面我们可以使用一种内置的拖动渐隐 ...
- 【BZOJ】3196: Tyvj 1730 二逼平衡树(区间第k小+树套树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3196 Treap+树状数组 1WA1A,好伤心,本来是可以直接1A的,这次开始我并没有看题解,就写出 ...
- android DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法
android DDMS 连接真机(己ROOT),用file explore看不到data/data文件夹的解决办法 问题是没有权限,用360手机助手或豌豆荚也是看不见的. 简单的办法是用RE文件管理 ...
- 转:mysql中int、bigint、smallint 和 tinyint的区别与长度的含义
bigint 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. P.S. b ...
- OpenCV 2.4.11 VS2010 Configuration
Add in the system Path: C:\opencv\build\x86\vc10\bin; Project->Project Property->Configuration ...
- 找1到n所有整数出现1的个数
编程之美2.4 n=12时,1,11,12这3个数包含1,所以1的个数是5. Line 9是为了防止factor溢出. #include <iostream> #include <s ...
- 【新产品发布】《EVC8021 RS-232<>RS-485/422 隔离接口转换器》
[数据手册下载] 1.百度云盘:(把下面蓝色连接复制到浏览器下打开) http://pan.baidu.com/s/1eQlJ0zC 2.淘宝公司的淘云盘:(点击下面连接后,需要用淘宝账户登录) ht ...
- linux查看时间和修改时间
查看当前时间,date -R 设置时间 date -s 例如当前时间2014年11月3日17:22:48 date -s 11/3/2014 date -s 17:22:48 先设置日期后设置具体时间 ...
- jquery.cookie.js 的使用
jquery.cookie.js 对cookie的操作 $.cookie('the_cookie'); //读取Cookie值 $.cookie('the_cookie', 'the_value'); ...