Java Learning -023-properties Class XML configuration file read and write source code

Source: Internet
Author: User

The previous properties article has described the basic usage of the Java configuration file class properties, and when viewing the Help documentation for the JDK, there are also two methods in the Properties class LoadFromXML (InputStream) and S Toretoxml (OutputStream, String, String), the XML in the method name is not difficult to determine that the two methods are read/write data to an XML file, respectively. The JDK documentation section looks like this:

Thus, this article demonstrates how the Properties class writes data to an XML file and how to read the data through a source instance. Of course, the data written to XML needs to conform to a certain standard, that is, in the form of key-value pairs, and the format of the resulting XML file can be referenced in PROPERTIES.DTD.

Words do not say much, small two on the code ... Please refer to the small master. If there is any deficiency, please ask the great God to correct me, I appreciate it!

The source code for creating the XML file is as follows:

1     /**2 * @function Write data to XML file by Properties3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java propertieswritexml, 2014-11-20 16:47:47 EXP $ 6      * 7      * @paramfilename:full Path8      * @paramAuthor:creator9      * @paramConfigs:file ContentsTen      * @returnBoolean One      */ A      Public BooleanPropertieswritexml (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 { AOutputStream OS =Newfileoutputstream (filename); at              -              for(string[] row:configs) { -Properties.setproperty (Row[0].trim (), row[1].trim ()); -             } -              -Properties.storetoxml (OS, "Author:" + author, "UTF-8"); in os.close (); -              toSuccess =true; +}Catch(IOException IoE) { -              This. Message = "file {" + filename + "} Write Failed! "; the              This. Logger.error ( This. message, IOE); *         } $         Panax Notoginseng         returnsuccess; -}
Properties write Java xml config file source code

Read the XML file source code as follows:

1     /**2 * @function Read XML file by Properties3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium main.java.aaron.java.tools Fileutils.java xmlreadbyproperties, 2014-11-20 16:46:53 EXP $ 6      * 7      * @paramFileName: File full path8      * 9      * @returnPropertiesTen      */ One      PublicProperties propertiesreadxml (String filename) { AProperties Properties =NewProperties (); -  -         /*parameter check: null or empty string when throwing argument illegal exception*/ the         if(FileName = =NULL|| ". Equals (filename) | |! This. Assertfiletype (FileName, "XML")) { -             Throw Newillegalargumentexception (); -         } -          +         Try { -             /*get File data stream*/ +InputStream is =Newfileinputstream (filename); A             /*load File data stream*/ at Properties.loadfromxml (IS); -             /*close File Data flow*/ - is.close (); -}Catch(IOException IoE) { -Properties =NULL; -              in              This. Message = "file {" + filename + "} Read failed! "; -              This. Logger.error ( This. message, IOE); to         } +          -         returnproperties; the}
Properties read Java XML config file source code

The test source is as follows:

1     /**2 * Test:write/read Data to/from XML file3      * 4      * @authorAARON.FFP5      * @versionv1.0.0:autouiselenium test.java.aaron.java.tools Fileutilstest.java test_propertieswritexml, 2014-11-20 16:53:53 EXP $6      *7      */8      Public voidTest_propertieswritereadxml () {9          This. Message = "\n\n\ntest:fileutils.propertieswritexml (string filename, string author, arraylist<string[]> configs )" +Ten"\ fileutils.propertiesreadxml (String filename)"; One          This. Logger.debug ( This. message); A          -          This. Fu =NewFileUtils (); -String filename = This. constantslist. Projecthome + This. constantslist. Fileseparator + the"Testng-temp" + This. constantslist. Fileseparator + "Test_propertieswritereadxml.xml"; -          -arraylist<string[]> configs =NewArraylist<string[]>(); -          +String author = "AARON.FFP"; -          +          for(inti = 0; I < 10; i++) { Astring[] row =NewString[2]; at              -Row[0] = i + ""; -ROW[1] = "The row number is" + (i + 1); -              - Configs.add (i, row); -         } in          -         if( This. Fu.propertieswritexml (filename, author, configs)) { toAssert.assertequals ( This. Fu.propertiesgetvalue ( This. Fu.propertiesreadxml (FileName), "2"), "The row number is 3" and "Test case failed."); +}Else { -              This. Message = "Test case failed, check the data file:" +filename; theAssert.fail ( This. message); *         }; $}
Properties read Java XML configuration file test source

The program execution results are as follows:

The contents of the generated XML file are as follows:

At this point, the Java learning -023-properties class to create an XML file 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 -023-properties Class XML configuration file read and write source code

Related Article

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.