Java Learning -022-properties File data writing

Source: Internet
Author: User

The properties profile is written primarily through Properties.setproperty and Properties.store two methods, this article writes a simple properties file to the source code to do the example.

Small two on the yards ... If there is any deficiency, please ask the great God to correct me, I appreciate it!

The Properties file is written to the source code as follows:

1     /**2 * @function Write data to the text file by Properties3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java propertieswrite, 2014-11-20 16:45:42 Exp $6      * 7      * @paramfilename:full path for config file8      * @paramAuthor:creator9      * @paramConfigs:file ContentsTen      * @returnBoolean One      */ A      Public BooleanPropertieswrite (string filename, string author, arraylist<string[]>configs) { -Properties Properties =NewProperties (); -         BooleanSuccess =false; the          -         /*parameter check: argument illegal, throw argument invalid exception*/ -         if(FileName = =NULL|| ". Equals (filename) | | Author = =NULL|| "". Equals (author) | |Configs.isempty ()) { -             Throw Newillegalargumentexception (); +         } -          +         Try { AFileOutputStream fos =Newfileoutputstream (filename); at              -              for(string[] row:configs) { -Properties.setproperty (row[0].tostring (). Trim (), row[1].tostring (). Trim ()); -             } -              -Properties.store (Fos, "Author:" +author); in fos.close (); -              toSuccess =true; +}Catch(IOException IoE) { -              This. Message = "file {" + filename + "} Write Failed! "; the              This. Logger.error ( This. message, IOE); *         } $         Panax Notoginseng         returnsuccess; -}
Properties File Write source

The test source is as follows:

1     /**2 * Test:store data to properties file3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java test_propertieswrite, 2014-11-20 16:49:42 EXP $6      *7      */8      Public voidTest_propertieswrite () {9          This. Message = "\n\n\ntest:fileutils.propertieswrite (string filename, string author, arraylist<string[]> configs)";Ten          This. Logger.debug ( This. message); One          A          This. Fu =NewFileUtils (); -String filename = This. constantslist. Projecthome + This. constantslist. Fileseparator + -"Testng-temp" + This. constantslist. Fileseparator + "Test_propertieswrite.properties"; the          -arraylist<string[]> configs =NewArraylist<string[]>(); -          -String author = "AARON.FFP"; +          -          for(inti = 0; I < 10; i++) { +string[] row =NewString[2]; A              atRow[0] = i + ""; -ROW[1] = "The row number is" + (i + 1); -              - Configs.add (i, row); -         } -          in         if( This. Fu.propertieswrite (filename, author, configs)) { -Assert.assertequals ( This. Fu.propertiesgetvalue ( This. Fu.propertiesread (FileName), "2"), "The row number is 3" and "Test case failed."); to}Else { +              This. Message = "Test case failed, check the data file:" +filename; -Assert.fail ( This. message); the         }; *}
Test Source

Test the Propertiesread (String) method called in the source code see: Get Properties Configuration file contents

Test the Propertiesgetvalue (Properties, key) method called in the source code see: Get the corresponding configuration item in the Properties configuration file

The propertieskeyisexist (Properties, Key) method called in the above method is described in: Determine if there is a corresponding configuration item in the Properties configuration file

The results of the test execution are as follows:

The contents of the file generated by the test are as follows:

At this point, Java learning -022-properties file data write successfully completed, 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 -022-properties File data writing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.