Properties and ResourceBundle, bundle. properties of attribute File Operations

Source: Internet
Author: User

Properties and ResourceBundle, bundle. properties of attribute File Operations

1. Properties and ResourceBundle

Both classes can read key-value pairs stored in the property file in the form of key/value. ResourceBundle is relatively simple to read the property file.

2. Properties

This class inherits Hashtable and stores key-value pairs in the collection. When the load () method is called, the key-value pair is read from the property file based on the input stream. The load () method does not close the input stream automatically and must be disabled manually.

/*** Read the attribute file based on the input stream: Properties inherits Hashtable, and the underlying key/value pairs are stored in the set, * The put method can be used to add a key-value pair to the set or modify the value of the key ** @ throws IOException */@ SuppressWarnings ("rawtypes") @ Test public void test01 () throws IOException {FileInputStream FD = new FileInputStream ("Files/test01.properties"); Properties props = new Properties (); props. load (FCM); // read all the content of the file to the memory, and the input stream reaches the end of the FCM. close (); // after loading is completed, the input stream is no longer used. The program is not automatically closed and needs to be manually closed/* byte [] buf = new byte [1024]; int length = Fi. read (buf); System. out. println ("content =" + new String (buf, 0, length); // throw StringIndexOutOfBoundsException */System. out. println ("driver =" + props. getProperty ("jdbc. driver "); System. out. println ("url =" + props. getProperty ("jdbc. url "); System. out. println ("username =" + props. getProperty ("jdbc. username "); System. out. println ("password =" + props. getProperty ("jdbc. password ");/*** Properties other methods that may be used */props. put ("serverTimezone", "UTC"); // The underlying layer uses hashtable. put (key, value) props. put ("jdbc. password "," 456 "); FileOutputStream fos = new FileOutputStream (" Files/test02.xml "); // write data in Hashtable to props in the xml file. storeToXML (fos, "Four database connection elements from attribute Files"); System. out. println (); System. out. println ("traversing Property Files"); System. out. println ("number of key pairs in hashtable =" + props. size (); Enumeration keys = props. propertyNames (); while (keys. hasMoreElements () {String key = (String) keys. nextElement (); System. out. println (key + "=" + props. getProperty (key ));}}
3. ResourceBundle

This class reads attribute files based on classes: the attribute files are treated as classes, meaning that the attribute files must be placed in the package. The full-qualified class names of the attribute files are used instead of the paths to refer to the attribute files.

/*** Read attribute files based on classes: This method treats attribute files as classes, and stores attribute files in packages, use the full limit of the attribute file instead of the path to refer to the file */@ Test public void test02 () {ResourceBundle bundle = ResourceBundle. getBundle ("com. javase. properties. test01 "); System. out. println ("Get the value of the specified key"); System. out. println ("driver =" + bundle. getString ("jdbc. driver "); System. out. println ("url =" + bundle. getString ("jdbc. url "); System. out. println ("username =" + bundle. getString ("jdbc. username "); System. out. println ("password =" + bundle. getString ("jdbc. password "); System. out. println ("-----------------------------"); System. out. println ("traversing Property Files"); Enumeration <String> keys = bundle. getKeys (); while (keys. hasMoreElements () {String key = keys. nextElement (); System. out. println (key + "=" + bundle. getString (key ));}}
4. Read the property file to the Spring container

Generally, the four elements of database connection are put in the attribute file, and the program reads the parameters from the attribute file. In this way, when the database connection element changes, the source code does not need to be modified. To load the content of an attribute file to an xml file:

5. Notes

# Put It In front to add comments to the property file.

6. Encoding

The property file uses the ISO-8859-1 encoding method, which does not support Chinese characters and Chinese characters are converted to Unicode encoding for display.

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.