Java Magic Hall: Read the. Properties configuration file

Source: Internet
Author: User

First, preface

Java Engineering in the LOG4J, database connections and other configuration information is generally written in the. properties file, then how to read these configuration information? The relevant methods are documented below for later inspection.

Ii.. properties File

A type of configuration file in which the content exists as a key-value pair, and each key-value pair is exclusive to one row. #号作为行注释的起始标志, Chinese annotations are automatically Unicode encoded. Example:

# IP and Port of server Socketip=127.0. 0.1 Port=9999# error messagemsg=i'm Sorry, bye bye!

Assume that the above content is stored under the Config.properties file, and the bin directory results as follows:

Bin

|--Main

|--Demo.class

|--config.properties

Examples of subsequent chapters will act as the target object for the above content.

Third, through the properties Object operation

Read Properties, example:

 Public classdemo{ Public Static voidMain (string[] args) {Properties props=NewProperties (); InputStreaminch= Demo.class. getResourceAsStream (".. /config.properties"); //or use a file input stream (not recommended), assuming the current working directory is bin//InputStream in = new FileInputStream ("./config.properties");Props.load (inch);
In.close (); //Read Specific PropertiesString key ="IP"; String IP=Props.getproperty (key); //iterate through all the properties, one waySet keys =Props.keyset (); for(Interator it =keys.iterator (); It.hasnext ();) {String k=It.next (); System. out. println (k +":"+Props.getproperty (k)); } //traverse All properties, mode twoEnumeration en =Props.propertynames (); while(En.hasmoreelements ()) {String K=en.nextelement (); System. out. println (k +":"+Props.getproperty (k)); } }}

1. Via Demo. Class. getResourceAsStream (".. /config.properties"); Read the configuration file, and the relative path of the configuration file is the directory where the class file is located as the current directory.

2. Through the new FileInputStream ("./config.properties"); Read the configuration file, the relative path of the configuration file to the working directory (can get working directory through System.getproperty ("user.dir") as the current directory.

Note: None of the configuration files obtained in either of these ways are cached. Reload the configuration file every time.

Write properties, example:

Properties props =NewProperties (); InputStreaminch= GetClass (). Getresouceasstream ("Properties File directory relative to the current class load path");p Rops.load (inch); OutputStream output=NewFileOutputStream ("Properties File Path");p Rops.setproperty ("IP","10.248.112.123");//Modify or add a property key value pairProps.store (Output,"Modify IP Value");//Store (outputstream output, String comment) writes the modified result to the output streamOutput.close ()

Iv. manipulating with ResourceBundle objects

This way, only the configuration file can be read, not write operations. Example:

// ResourceBundle RB = resourcebundle.getbundle ("Relative path of the configuration file relative to the project root (without extension)"); ResourceBundle RB = Resourcebundle.getbundle ("config"); Try {    = rb.getstring ("name");} Catch (MissingResourceException ex) {

Note: The configuration file information is cached in the above manner, and subsequent reads are read in the cache, and if the configuration is modified during this period, it cannot be synchronized in real time.

ResourceBundle has two subclasses listresourcebundle and propertyResourceBundle, which are actually handled using propertyResourceBundle when reading the properties file.

Off Topic:

ResourceBundle is primarily used to address internationalization and localization issues. The information for each language and dialect is defined by resource naming, and the program obtains the current localization information at run time, and the appropriate resources are loaded according to localization information to complete localization.

Resource Naming conventions:

// with family name only MyResource // family name and language included myresource_en // includes family name, language and country Myresource_en_us

The corresponding Java code:

// ResourceBundle First will be based on the language and the country's localization information to find resources (assuming now to find MYRESOURCE_ZH_CN), when not found will find Myresource_zh, and can not find the use of MyResource. ResourceBundle RB = Resourcebundle.getbundle ("myresource", Locale.getdefault ())

V. Summary

Of course there are more ways than this, and continue to add!

Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/3995386.html ^_^ Fat Boy John

Vi. references

Http://www.cnblogs.com/panjun-Donet/archive/2009/07/17/1525597.html

Java Magic Hall: Read the. Properties configuration file

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.