码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习-020-Properties 判断是否存在对应的 key 项

时间:2015-06-29 19:47:07      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

在日常的脚本编写过程中,通常会判断配置文件中是否存在对应的配置项,以判断是否执行相应的业务逻辑。

小二上码。。。若有不足之处,敬请大神指正,不胜感激!

判断是否存在 key 项(配置项)的方法源码如下所示:

技术分享
 1     /**
 2      * Verify the key contains in properties or not
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesKeyIsExist, 2014-11-20 16:31:10 Exp $
 6      * 
 7      * @param prop : properties
 8      * @param key  : key
 9      * @return boolean
10      */
11     public boolean propertiesKeyIsExist(Properties prop, String key){
12         boolean success = false;
13         String item = "";
14         
15         // verify the properties file is null
16         if (prop == null) {
17             this.message = "The content of properties file is null !";
18             this.logger.error(this.message);
19             
20             success = false;
21             return success;
22         }
23         
24         // verify the key is null
25         if ("".equals(key) || key == null) {
26             this.message = "There is no key {" + key + "} in properties config file.";
27             this.logger.error(this.message);
28             
29             success = false;
30             return success;
31         }
32         
33         // get keys from properties
34         Enumeration<?> enu = prop.propertyNames();
35 
36         // verify the key is contains in properties or not
37         while (enu.hasMoreElements()) {
38             item = (String)enu.nextElement();
39             
40             if (item.equals(key)) {
41                 success = true;
42             }
43         }
44         
45         return success;
46     }
判断 properties 配置文件是否存在相应的配置项源码

测试源码如下所示:

技术分享
 1     /**
 2      * Test : Verify the key contains in properties file or not
 3      * 
 4      * @author Aaron.ffp
 5      * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesKeyIsExist, 2014-11-20 16:35:15 Exp $
 6      *
 7      */
 8     public void test_propertiesKeyIsExist(){
 9         this.message = "\n\n\nTEST:FileUtils.propertiesKeyIsExist(Properties prop, String key)";
10         this.logger.debug(this.message);
11         
12         this.fu = new FileUtils();
13         String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 
14                           "testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties";
15         
16         Properties prop = this.fu.propertiesRead(filename);
17         
18         // print-1
19         prop.list(System.out);
20         
21         System.out.println("\n\n");
22         
23         Assert.assertEquals(this.fu.propertiesKeyIsExist(prop, "host"), true, "Test case failed.");
24     }
测试源代码

 

至此, Java学习-020-Properties 判断是否存在对应的 key 项 顺利完结,希望此文能够给初学 Java 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

Java学习-020-Properties 判断是否存在对应的 key 项

标签:

原文地址:http://www.cnblogs.com/fengpingfan/p/4607904.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!