Properties how-to guide

Source: Internet
Author: User

A Brief introduction:

Properties is a much more class used in Java, representing a persistent set of properties. Inherited from Hashtable,properties can be loaded from a stream or saved in a stream. Each key in the attribute list is a string that is extremely corresponding to the value. It can be understood as a form of key-value.

Many times in order to improve the application efficiency of the program, such properties files can be stored in the cache system, so in the operation of some unique data can choose to use the properties. In the development process, you need to use it flexibly. The use of the class is described in detail in this blog post, as well as saving the properties file as an XML file and reading the key value in the XML file, and so on, because in this case, the code uses a detailed comment, so in order to reduce space, leaving only the information useful to understand the code, so no more elaboration.

This Boventh my labor achievement income, in the Blog Park summary share, welcome reprint. The statement of this paragraph must be retained without the written instructions of the author. itred Email:[email protected] Blog link:http://www.cnblogs.com/itred GitHub Link:/http Github.com/itred

Two Case:

    1. New project:

In this demo, the creation process of the file is not described, and is created directly by hand. Create a new Java project and, in the project directory, create a new properties file to hold the string we are about to manipulate (a key-value pair string that can be understood as key-value).

  2. Add a Key-value string:

The main idea is to load the properties file first, in which it is important to note that this write must be saved, so you need not only the input stream, but also the output stream. Use SetProperty to type the Key-value value and save it using the store. The key code is as follows:

New    Bufferedinputstream (new  FileInputStream (Props_path)); Props.load (in);        New Bufferedoutputstream (new  FileOutputStream (Props_path));       Props.setproperty (key, value);       " INSERT key-value: "+ KEY +" = "+ VALUE +" * * * ");

3. Write all the key-value in map to the properties:

Because map is also a key-value pair of type Key-value, the properties can be approximated. To implement writing all the values in the map to the properties file, the main idea is the same as above, except that you need to traverse all the key and value values in the map when writing. The key code is as follows:

New Bufferedinputstream (new  FileInputStream (Props_path));       Props.load (in);        New Bufferedoutputstream (new  FileOutputStream (Props_path));         for (map.entry<string, string> entry:map.entrySet ()) {             Props.setproperty (Entry.getkey (), Entry.getvalue ());       }        " Insert all map ");

 4. Traverse all the key and value values in the properties:

Load it directly into memory as a stream and get all the values directly. In fact, there are many ways to implement this traversal, and this example uses the following key code:

New Bufferedinputstream (new  FileInputStream (Props_path));       Props.load (in);        = props.propertynames ();       Props.list (System.out);         while (Enu.hasmoreelements ()) {          System.out.println ("(KEY)" + enu.nextelement () + "= (VALUE)"                     +  Props.getproperty (String) enu.nextelement ());        }

5. Get the map value from the key value:

Since it is almost always the key-value of a key-value pair string in the form of a map, it is natural to think of only the value of a key in the file. Just need Props.getproperty (key) to complete.

 6. Save the Key-value in the properties file as an XML file:

The idea of implementation is to directly call a method in the Properties Class Storetoxml () can be completed successfully. The key code is as follows:

New Bufferedinputstream (new  FileInputStream (Props_path));       Props.load (in);        New Bufferedoutputstream ( new FileOutputStream                   ("D:/test.xml"));       "Test");

 7. Get the value of key from the XML:

The XML file is loaded as a stream and then read into the properties via the method LoadFromXML () and then GetProperty ().

Properties props =NewProperties (); String Xml_path= "D:/test.xml"; InputStream in=NULL; Try{ in=NewBufferedinputstream (NewFileInputStream (NewFile (Xml_path));           Props.loadfromxml (in); String value=Props.getproperty (key); returnvalue; } Catch(Exception e) {e.printstacktrace (); return""; }

Three Summarize:

In fact, the use of properties is very simple, but for those who have never used this kind of friends, really do not know where to start. In this, I also have deep experience. Instead of using a method to query a method, it might as well be a one-time approach to the properties file in a comprehensive understanding, so that in the days to come, it is also handy, very convenient.

This case is my study summary, code implementation only for functional testing use, not for the production process, because, the continuous flow of file loading open, in this case, not in a timely shutdown operation. Therefore, if you need to apply to the actual production, you need to close the properties file in the Finally, in a timely manner.

Also attached to the case of the case involved in the source link: http://github.com/itred

   itred   blog:   http:        //      itred.cnblogs.com     
github: https://github.com/itRed Copyright Notice: This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph of instructions,
And in the article obvious position to give the original link, otherwise reserves the right to pursue legal responsibility.

Properties how-to guide

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.