During routine scripting, you typically get configuration items in the configuration file to execute the appropriate business logic.
Small two on the yards ... If there is any deficiency, please ask the great God to correct me, I appreciate it!
The source code for getting the configuration item value is as follows:
1 /**2 * Get value from properties by key. Return null When the key is not exist.3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java propertiesgetvalue, 2014-11-20 16:31:22 EXP $ 6 * 7 * @paramprop:properties8 * @paramKey:key9 * @returnStringTen */ One Publicstring Propertiesgetvalue (Properties prop, String key) { AString value = ""; - - if( This. Propertieskeyisexist (prop, key)) { theValue =Prop.getproperty (key); -}Else { -Value =NULL; - } + - returnvalue; +}Java Get Properties config file source code
The test source is as follows:
1 /**2 * Test:get value from properties file by key3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java test_propertiesgetvalue, 2014-11-20 16:40:15 EXP $6 *7 */8 @Test9 Public voidTest_propertiesgetvalue () {Ten This. Message = "\n\n\ntest:fileutils.propertiesgetvalue (Properties prop, String key)"; One This. Logger.debug ( This. message); A - This. Fu =NewFileUtils (); -String filename = This. constantslist. Projecthome + This. constantslist. Fileseparator + the"Testng-temp" + This. constantslist. Fileseparator + "Propertiesread.properties"; - -Properties prop = This. Fu.propertiesread (filename); - + //print-1 - prop.list (System.out); + ASystem.out.println ("\ n")); at -Assert.assertequals ( This. Fu.propertiesgetvalue (prop, "host"), "127.0.0.1", "Test case failed."); -}Test Source
The execution results are as follows:
At this point, Java learning -021-properties get configuration items corresponding to the value of a smooth end, I hope this article can give beginners Java you a reference.
Finally, very grateful to the pro-stop, I hope this article can be pro helpful. Warmly welcome the kiss to discuss together and progress together. Thank you so much! ^_^
Java Learning -021-properties Get a value for a configuration item