Title, the properties configuration file is often used in projects, so what are the ways to read the properties configuration file?

Source: Internet
Author: User


Method One: You can pass the load () method of the Java.util.Properties class

1 InputStreamin=lnewBufferedInputStream(newFileInputStream(name));
2 Propertiesp=newProperties();
3 p.load(in);

Method Two: Use spring to read the properties configuration file
Org.springframework.beans.factory.support.PropertiesBeanDefinitionReader


Method Three: Through the Getbundle () method of the Java.util.ResourceBundle class

1 ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

Method Four: Through the constructor of the Java.util.PropertyResourceBundle class

1 InputStream in = newBufferedInputStream(newFileInputStream(name));
2 ResourceBundle rb = newPropertyResourceBundle(in);

Thanks to a method provided by @luochen1314
Version two:

ResourceBundle res = Resourcebundle.getbundle (baseName);
BaseName is the package path/file name (without suffix)
For example, the properties file is under the COM.TT package and the file name is Tt.properties
Then Basenamewei COM/TT/TT


Method Five: Use the getResourceAsStream () method of the class variable

1 InputStream in = JProperties.class.getResourceAsStream(name);
2 Properties p = newProperties();
3 p.load(in);

Method Six: The getResourceAsStream () method of Java.lang.ClassLoader obtained by Class.getclassloader ()

Previewing source code printing
1 InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
2 Properties p = newProperties();
3 p.load(in);

Method Seven: Through the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class

1 InputStream in = ClassLoader.getSystemResourceAsStream(name);
2 Properties p = newProperties();
3 p.load(in);

Method Eight: You can use the Javax.servlet.ServletContext getResourceAsStream () method in a servlet

1 InputStream in = context.getResourceAsStream(path);
2 Properties p = newProperties();
3 p.load(in);

Title, the properties configuration file is often used in projects, so what are the ways to read the properties configuration file?

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.