During routine scripting, it is common to determine if there are any corresponding configuration items in the configuration file to determine whether the appropriate business logic is being executed.
Small two on the yards ... If there is any deficiency, please ask the great God to correct me, I appreciate it!
The way to determine if a key entry (configuration item) exists is the source code as follows:
1 /**2 * Verify The key contains in properties or not3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java propertieskeyisexist, 2014-11-20 16:31:10 EXP $ 6 * 7 * @paramprop:properties8 * @paramKey:key9 * @returnBooleanTen */ One Public Booleanpropertieskeyisexist (Properties prop, String key) { A BooleanSuccess =false; -String item = ""; - the //Verify the properties file is null - if(Prop = =NULL) { - This. Message = "The content of the properties file is null!"; - This. Logger.error ( This. message); + -Success =false; + returnsuccess; A } at - //Verify the key is null - if("". Equals (key) | | Key = =NULL) { - This. Message = "There is no key {" + key + "} in the properties config file."; - This. Logger.error ( This. message); - inSuccess =false; - returnsuccess; to } + - //get keys from properties theEnumeration<?> ENU =prop.propertynames (); * $ //Verify the key is contains in properties or notPanax Notoginseng while(Enu.hasmoreelements ()) { -item =(String) enu.nextelement (); the + if(Item.equals (key)) { ASuccess =true; the } + } - $ returnsuccess; $}
determine if the properties configuration file has a corresponding configuration item source code
The test source is as follows:
1 /**2 * Test:verify The key contains in properties file or not3 * 4 * @authorAARON.FFP5 * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java test_propertieskeyisexist, 2014-11-20 16:35:15 EXP $6 *7 */8 Public voidtest_propertieskeyisexist () {9 This. Message = "\n\n\ntest:fileutils.propertieskeyisexist (Properties prop, String key)";Ten This. Logger.debug ( This. message); One A This. Fu =NewFileUtils (); -String filename = This. constantslist. Projecthome + This. constantslist. Fileseparator + -"Testng-temp" + This. constantslist. Fileseparator + "Propertiesread.properties"; the -Properties prop = This. Fu.propertiesread (filename); - - //print-1 + prop.list (System.out); - +System.out.println ("\ n")); A atAssert.assertequals ( This. fu.propertieskeyisexist (prop, "host"),true, "Test case failed."); -}
Test Source Code
At this point, Java learning -020-properties to determine if there is a corresponding key key to the successful completion, 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 -020-properties Determine if there is a corresponding key key