在日常的脚本编写过程中,通常会获取配置文件中的配置项,以执行相应的业务逻辑。
小二上码。。。若有不足之处,敬请大神指正,不胜感激!
获取配置项值的源码如下所示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Get value from properties by key. Return null when the key not exist. 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoUISelenium main.java.aaron.java.tools FileUtils.java propertiesGetValue, 2014-11-20 16:31:22 Exp $ 6 * 7 * @param prop : properties 8 * @param key : key 9 * @return String 10 */ 11 public String propertiesGetValue(Properties prop, String key){ 12 String value = ""; 13 14 if (this.propertiesKeyIsExist(prop, key)) { 15 value = prop.getProperty(key); 16 } else { 17 value = null; 18 } 19 20 return value; 21 }
测试源码如下所示:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * Test : Get value from properties file by key 3 * 4 * @author Aaron.ffp 5 * @version V1.0.0: autoUISelenium test.java.aaron.java.tools FileUtilsTest.java test_propertiesGetValue, 2014-11-20 16:40:15 Exp $ 6 * 7 */ 8 @Test 9 public void test_propertiesGetValue(){ 10 this.message = " TEST:FileUtils.propertiesGetValue(Properties prop, String key)"; 11 this.logger.debug(this.message); 12 13 this.fu = new FileUtils(); 14 String filename = this.constantslist.PROJECTHOME + this.constantslist.FILESEPARATOR + 15 "testng-temp" + this.constantslist.FILESEPARATOR + "propertiesRead.properties"; 16 17 Properties prop = this.fu.propertiesRead(filename); 18 19 // print-1 20 prop.list(System.out); 21 22 System.out.println(" "); 23 24 Assert.assertEquals(this.fu.propertiesGetValue(prop, "host"), "127.0.0.1", "Test case failed."); 25 }
执行结果如下所示:
至此, Java学习-021-Properties 获取配置项对应的值 顺利完结,希望此文能够给初学 Java 的您一份参考。
最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^