Use of the properties class in Java

Source: Internet
Author: User

1.properties Introduction

The properties file in Java is a configuration file that is used primarily to express configuration information with a file type of *. Properties, formatted as text files, the contents of the file is formatted as "key = value" in the format, in the properties

File, you can use "#" to make comments,properties files in Java programming in a lot of places, easy to operate. The class is a subclass of Hashtable so map combines the basic use of it all, but what we need is the following amount:

2. Special methods

    A. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.

    B. public void load (reader reader) to read the list of attributes (keys and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file (for example, the Test.properties file above). To search for GetProperty (String key).

    c. SetProperty (string key, String value), calls the Hashtable method put. It is simply that the put (k key,v value) is guaranteed to accept only string arguments by explicitly specifying the string type, and we often need to use this method to wrap the method of a parent class when actually developing.

    d. public void Store (writer writer, String comments), in contrast to the Load method, writes a key-value pair to the specified file.

    E. public set<string> Stringpropertynames () returns the set of keys in this property list, where the key and its corresponding value are strings, primarily for traversing

---------------------below gives a demo with the IO stream, which is explained in detail .....

1 ImportJava.io.FileReader;2 ImportJava.io.FileWriter;3 Importjava.io.IOException;4 ImportJava.io.Reader;5 ImportJava.io.Writer;6 Importjava.util.Properties;7 8 /*9 * The collection here must be the propertiesTen * public void load (Reader reader): reads the data from the file into the collection One * public void Store (writer writer, String comments): Stores the data in the collection to a file A  *  - * Action to achieve game progress saved on load -  */ the  Public classPropertiesDemo1 { -      Public Static voidMain (string[] args)throwsIOException { -         //myload (); //load the data into the collection, load loads the -Mystore ();//stores the data in the collection to a file, store writes +     } -  +     Private Static voidMystore ()throwsIOException { A         //To create a collection object atProperties prop =NewProperties (); -Prop.setproperty ("Jav", "1"); -Prop.setproperty ("C + +", "2"); -Prop.setproperty ("JVM", "3"); -          -         //Public void Store (writer writer, String comments): Stores the data in the collection to a file inWriter W =NewFileWriter ("Name.txt"); -Prop.store (W,NULL); to w.close (); +     } -  the     Private Static voidMyload ()throwsIOException { *         //To create a collection object $Properties prop =NewProperties ();Panax Notoginseng  -         //Public void Load (Reader reader): reads the data from the file into the collection the         //and this file must be in the form of a key-value pair! There are formatting requirements here +Reader r =NewFileReader ("A.txt"); A Prop.load (r); the r.close (); +  - System.out.println (prop); $     } $}

//---------------

The following example uses a file that is the A.txt file created by the demo above. Create your own files in Eclipse and write c=1 on the inside c++=2 java=3.

1 ImportJava.io.FileReader;2 ImportJava.io.FileWriter;3 Importjava.io.IOException;4 ImportJava.io.Reader;5 ImportJava.io.Writer;6 Importjava.util.Properties;7 ImportJava.util.Set;8 9 /*Ten * I have a text file (user.txt) and I know that the data is in the form of a key-value pair, but I don't know what the content is.  One * Please write a program to determine if there is a "C + +" Such a key exists, if there is a change in fact, " " A  *  - * Analysis: - * A: Load the data in the file into the collection the * B: Iterate through the collection to get each key - * C: Determine if the key is "C + +", if any, modify its value to " " - * D: Re-store the data in the collection into a file -  */ +  Public classPropertiesDemo2 { -      Public Static voidMain (string[] args)throwsIOException { +         //To create a collection object AProperties prop =NewProperties (); at         //read file to collection -Reader r =NewFileReader ("Name.txt"); - Prop.load (r); - r.close (); -         //traversing get keys and values -set<string> set = Prop.stringpropertynames ();//gets the collection of keys in          for(String key:set) { -             if(Key.equals ("C + +")) { toString value =Prop.getproperty (key); +Prop.setproperty (Key, "100");//set the specified key to -                 //because the key is not repeatable, so modify it once!!  the                  Break; *             } $         }Panax Notoginseng  -         //to write the modified collection elements back to the file theWriter W =NewFileWriter ("Name.txt"); +Prop.store (W,NULL); A w.close (); the     } +}

Use of the properties class in Java

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.