Disclaimer: The properties files in all the examples in this article are placed in the SRC directory, and the Ecclipse software automatically increases
First, the basic concept
1.1
Properties file, which stores the format key = value.
Properties File Features:
1. Key-value pair format
2, "=" After the equal sign, the value in front of the space, will be automatically ignored
3, the space after the value is not ignored
4, the double quotation mark after the "=" equals sign, does not ignore
5, "#" Well after the content, for comments, ignore
1.2 Java Properties Class attribute map (property map)
is a data structure that stores key/value pairs. Attribute mappings are often used to store configuration information.
It has three properties:
1. key and value morale strings
2. key/value pairs can be easily written to or read from a file
3. Use a Level two table to store default values
Java classes that implement attribute mappings are called properties (Java.util.Properties), which are the more important classes in Java.
Mainly used to read Java configuration files, various languages have their own supported configuration files, many variables in the configuration file are often changed,
This is also for the convenience of users, so that users can be removed from the program itself to modify the relevant variable settings, improve the scalability of the program.
This class is thread-safe: Multiple threads can share a single Properties object without having to synchronize externally.
Construction Method:
-
- Properties () Creates a list of empty properties with no default values.
- Properties (properties defaults) creates a list of empty properties with the specified default value.
It provides several main methods:
GetProperty (String key): 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.
load (InputStream instream): Reads the list of attributes (key 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).
SetProperty (string key, String value) : The underlying method of calling Hashtable is put. He sets a key-value pair by calling the put method of the base class.
Store (OutputStream out, String comments): Writes the list of attributes (key and element pairs) in this properties table to the output stream in a format that is appropriate for loading into the properties table using the Load method. In contrast to the load method, the method writes a key-value pair to the specified file.
Clear (): Clears all loaded key-value pairs. This method is provided in the base class.
Note: Because properties inherit from Hashtable, you can apply the put and Putall methods to the Properties object. However, these two methods are not recommended because they allow the caller to insert an item whose key or value is not a String. instead, you should use the SetProperty method.
If the store or save method is called on an unsafe Properties object (that is, a key or value that contains a non-String), the call fails.
Similarly, if the PropertyNames or list method is called on an "unsafe" Properties object (that is, a key that contains a non-String), the call will fail.
Second, Java read the properties file method
2.1 Java Virtual Machine (JVM) has its own system configuration file (System.properties)
// Get System Properties for the JVM Import java.util.Properties; Public class READJVM { publicstaticvoid main (string[] args) { = system.getproperties (); Pps.list (System.out); } }
2.2 Six ways to read the Properties file using the J2SE API
Method One: The load () method of the Java.util.Properties class
InputStream in = new Bufferedinputstream (new FileInputStream (name));
Properties P = new properties ();
P.load (in);
/*** Note: The configuration file must be placed in the root directory of the project. */@Test Public voidrun1 () {Try{FileInputStream is=NewFileInputStream ("src/my-ini.properties"); Properties Pop=NewProperties (); Try{pop.load (IS); } Catch(IOException e) {e.printstacktrace (); } Enumeration em=Pop.propertynames (); while(Em.hasmoreelements ()) {String key=(String) em.nextelement (); String value=Pop.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"="+value); } } Catch(FileNotFoundException e) {e.printstacktrace (); } }
Method Two: Using the class variable getResourceAsStream () method
InputStream in = JProperties.class.getResourceAsStream (name);
Properties P = new properties ();
P.load (in);
/*** Note: The configuration file must be placed in the current directory. * (the directory hierarchy can also start with the folder below SRC, without having to include SRC, and does not have to include a backslash.) ) * This method is in the PropertiesDemo1 class*/@Test Public voidrun2 () {Properties pop=NewProperties (); Try{InputStream in= PropertiesDemo1.class. getResourceAsStream ("/my-ini.properties"); Pop.load (in); }Catch(Exception ex) {ex.printstacktrace (); } Enumeration em=Pop.propertynames (); while(Em.hasmoreelements ()) {String key=(String) em.nextelement (); String value=Pop.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"="+value); } }
Method Three: Using Class.getclassloader () to obtain the Java.lang.ClassLoader getResourceAsStream () method
InputStream in = JProperties.class.getClassLoader (). getResourceAsStream (name);
Properties P = new properties ();
P.load (in);
/*** NOTE: * The configuration file must be placed in the bin directory
* Note that the Eclipse software automatically copies the configuration files in src to the bin directory*/@Test Public voidRun3 () {Properties pop=NewProperties (); Try{InputStream in= PropertiesDemo1.class. getClassLoader (). getResourceAsStream ("my-ini.properties"); Pop.load (in); }Catch(Exception ex) {ex.printstacktrace (); } Enumeration em=Pop.propertynames (); while(Em.hasmoreelements ()) {String key=(String) em.nextelement (); String value=Pop.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"="+value); } }
Method Four: Use the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class
InputStream in = Classloader.getsystemresourceasstream (name);
Properties P = new properties ();
P.load (in);
/*** NOTE: * The configuration file must be placed in the bin directory*/@Test Public voidRun4 () {Properties pop=NewProperties (); Try{InputStream in= Classloader.getsystemresourceasstream ("my-ini.properties"); Pop.load (in); }Catch(Exception ex) {ex.printstacktrace (); } Enumeration em=Pop.propertynames (); while(Em.hasmoreelements ()) {String key=(String) em.nextelement (); String value=Pop.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"="+value); } }
Method Five: Use the Getbundle () method of the Java.util.ResourceBundle class
ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
/*** NOTE: * The configuration file must be placed in the bin directory*/@Test Public voidrun5 () {ResourceBundle rs= Resourcebundle.getbundle ("My-ini"); String Driver= rs.getstring ("Driver"); String URL= rs.getstring ("url"); String User= rs.getstring ("User"); String Password= rs.getstring ("Password"); System.out.println ("Driver=" +driver); System.out.println ("Url=" +URL); System.out.println ("User=" +user); System.out.println ("Password=" +password); }
Method Six: Use the constructor of the Java.util.PropertyResourceBundle class
@Test Public voidrun6 () {File file=NewFile ("src/my-ini.properties"); Try{Bufferedinputstream in=NewBufferedinputstream (Newfileinputstream (file)); Try{propertyresourcebundle prb=NewpropertyResourceBundle (in); String Driver= prb.getstring ("Driver"); String URL= prb.getstring ("url"); String User= prb.getstring ("User"); String Password= prb.getstring ("Password"); System.out.println ("Driver=" +driver); System.out.println ("Url=" +URL); System.out.println ("User=" +user); System.out.println ("Password=" +password); } Catch(IOException e) {e.printstacktrace (); } } Catch(FileNotFoundException e) {e.printstacktrace (); } }
Reference Blog:
Java Operations Properties configuration file--Summary (locale&resourcebundle& propertyResourceBundle (http://blog.csdn.net/ fanxiaobin577328725/article/details/52071310)
Properties Profile Read Operation Summary "Java notes"