zoukankan      html  css  js  c++  java
  • Java学习-021-Properties 获取配置项对应的值

    在日常的脚本编写过程中,通常会获取配置文件中的配置项,以执行相应的业务逻辑。

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

    获取配置项值的源码如下所示:

     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     }
    Java 获取 properties 配置文件源码

    测试源码如下所示:

     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 的您一份参考。

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

  • 相关阅读:
    在android模拟器运行arm 移植的 c程序
    Android实现对c++方式调用
    Hdu 1003 Max Sum
    HDU 1005 Number Sequence
    poj 1222 EXTENDED LIGHTS OUT 高斯消元法
    分治算法
    HDU杭电acm题目分类大全
    HDU 1002 A + B Problem II 大数相加
    HDU 1004 Let the Balloon Rise
    phpcms栏目和专题的区分
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4607939.html
Copyright © 2011-2022 走看看