Java read. Properties configuration file Several methods

Source: Internet
Author: User

Read. Properties configuration file in the actual development of the use of a lot, summed up a bit, there are several methods (just I know):
First, the Java.util.Properties class provided through the JDK.
This class inherits from Java.util.HashTable, which implements the map interface, so you can manipulate the properties file using the appropriate method, but it is not recommended to use two methods like put, Putall, because the put method not only allows to deposit string type value, It can also be stored in object type. So the Java.util.Properties class provides the GetProperty () and SetProperty () methods to manipulate the properties file while using store or save (obsolete) To save the property value (write the property value to the. Properties profile). Before using it, you also need to load the properties file, which provides two methods: Load and LoadFromXML.
Load has two methods of overloading: Load (inputstream instream), Load (reader reader), so you can load the properties file in different ways.
InputStream can be obtained according to different ways, such as:
1, through the current class loader getResourceAsStream method to obtain

[Java]View PlainCopy
    1. InputStream instream = testproperties.  Class.getclassloader (). getResourceAsStream ("test.properties");



2. Get from File

[Java]View PlainCopy
    1. InputStream instream = new FileInputStream (new File ("FilePath"));



3, also through the class loader to obtain, and the first kind of

[Java]View PlainCopy
    1. InputStream in = Classloader.getsystemresourceasstream ("FilePath");



4, in the servlet, you can also use the context to get InputStream

[Java]View PlainCopy
    1. InputStream in = Context.getresourceasstream ("FilePath");



5, through the URL to get

[Java]View PlainCopy
    1. URL url = new URL ("path");
    2. InputStream instream = Url.openstream ();



The Read method is as follows:

[Java]View PlainCopy
    1. Properties prop = new properties ();
    2. Prop.load (instream);
    3. String key = Prop.getproperty ("username");
    4. String key = (string) prop.get ("username");




Second, through the Java.util.ResourceBundle class to read, this way than the use of properties to facilitate some.
1, by Resourcebundle.getbundle () static method to obtain (ResourceBundle is an abstract class), this way to get the Properties property file does not need to add the. Properties suffix name, only the file name is required.

[Java]View PlainCopy
    1. ResourceBundle resource = Resourcebundle.getbundle ("com/mmq/test"); Test is the property file name, placed under the package COM.MMQ, if it is placed under SRC, directly with test can
    2. String key = resource.getstring ("username");



2, read from the InputStream, get InputStream method and the same, no longer repeat.

[Java]View PlainCopy
    1. ResourceBundle resource = new propertyResourceBundle (instream);



Note: The biggest problem encountered in the use of the configuration file may be the path problem, if the configuration file in the current class is located under the package, you need to use the package name qualification, such as: Test.properties into the COM.MMQ package, you want to use com/mmq/ Test.properties (obtained through properties) or Com/mmq/test (obtained by ResourceBundle); The properties file is in the SRC root, and you can use test.properties or test directly.

Java read. Properties configuration file Several methods

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.