JAVA magic Hall: Read the. properties configuration file

Source: Internet
Author: User

JAVA magic Hall: Read the. properties configuration file
I. Preface in Java projects, configuration information such as log4j and database connection is generally written in the. properties file. How can I read the configuration information? Next we will record the relevant methods for future reference. 2. One type of configuration file in the. properties file. The content exists in the form of a key-value pair, and each key-Value Pair excludes one row.. Example: # ip and port of server socketip = 127.0.0.1port = 9999 # error messagemsg = I'm sorry, bye! Assume that the above content is stored in config. the results of the bin directory under the properties file are as follows: bin | -- main | -- Demo. class | -- config. the examples in subsequent sections of properties operate on the above content as the target object. 3. Read Properties through the Properties object operation. For example, copy the code public class Demo {public static void main (String [] args) {Properties props = new Properties (); inputStream in = Demo. class. getResourceAsStream (".. /config. properties "); // or use the file input stream (not recommended). Assume that the current working directory is bin // InputStream in = new FileInputStream (". /config. properties "); props. load (in); in. close (); // read the String key = "ip"; String ip = props. getProperty (key); // traverses all attributes. Type 1 Set keys = props. keySet (); for (Interator it = keys. iterator (); it. hasNext ();) {String k = it. next (); System. out. println (k + ":" + props. getProperty (k);} // traverses all attributes. Method 2: Enumeration en = props. propertyNames (); while (en. hasMoreElements () {String k = en. nextElement (); System. out. println (k + ":" + props. getProperty (k) ;}} copy Code 1. through Demo. class. getResourceAsStream (".. /config. properties "); read the configuration file The relative path of the configuration file uses the directory where the class file is located as the current directory. 2. use new FileInputStream (". /config. properties "); read the configuration file, the relative path of the configuration file to the working directory (you can use the System. getProperty ("user. dir ") Get the working directory) as the current directory. Note: The configuration files obtained in the preceding two methods are not cached. Reload the configuration file every time. Write Properties. For example, copy the code Properties props = new Properties (); InputStream in = getClass (). getResouceAsStream ("directory of the properties file to the current class loading path"); props. load (in); OutputStream output = new FileOutputStream ("properties file path"); props. setProperty ("ip", "10.248.112.123"); // modify or add a property key value pair props. store (output, "modify ip value"); // store (OutputStream output, String comment) writes the Modification result to the output stream output. close () Copy Code 4. operate through the ResourceBundle object This method can only read the configuration file and does not allow write operations. Example: // ResourceBundle rb = ResourceBundle. getBundle ("relative path of the configuration file to the root directory of the project (excluding the extension)"); ResourceBundle rb = ResourceBundle. getBundle ("config"); try {String name = rb. getString ("name");} catch (MissingResourceException ex) {Note: the configuration file information will be cached in the preceding method, and the content in the cache will be read later, if the configuration content is modified during this period, the ResourceBundle that cannot be synchronized in real time has two sub-classes: ListResourceBundle and PropertyResourceBundle. when reading the properties file, PropertyResourceBundle is actually used for processing. Question: ResourceBundle is mainly used to solve internationalization and localization problems. The resource name defines the information of each language and dialect. However, the program obtains the current localization information at runtime and loads the corresponding resources according to the localization information to complete localization. Resource naming rules: Copy code // contains only the family name MyResource // contains the family name and language MyResource_en // contains the Java code corresponding to the family name, language, and country MyResource_en_US copy code: // ResourceBundle first searches for resources based on the localized information of the language and country (assuming MyResource_zh_CN is to be searched now). If it cannot be found, it will find MyResource_zh. If it cannot be found, MyResource will be used. ResourceBundle rb = ResourceBundle. getBundle ("MyResource", Locale. getDefault ())

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.