Java Read properties configuration file [Go]

Source: Internet
Author: User

Several common ways to read. properties files in online articles

1. Example of the load () method using the Java.util.Properties class :

InputStream in = Lnew bufferedinputstream (new  FileInputStream (name));    New Properties ();   

2. Using the Getbundle () method of the Java.util.ResourceBundle class

ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());   

Use ResourceBundle to read. properties files to avoid path problems
When I read the. properties file in the jar, I always can't find the file path and later read the. properties file with ResourceBundle to avoid the path problem, the code is as follows:


Process is the file name, remember not to add. Properties, URL is the key name in the file

ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process");      = Bundle.getstring ("URL");  System.out.println (s);   = S;  

3. Use the constructor of the Java.util.PropertyResourceBundle class

New Bufferedinputstream (new  FileInputStream (name));    New propertyResourceBundle (in);   

4. The getResourceAsStream () method using class variables

InputStream in = class name. class . getResourceAsStream (name);    New Properties ();   P.load (in);

5. Example of the Java.lang.ClassLoader getResourceAsStream () method obtained using Class.getclassloader () :

InputStream in = class name. class . getClassLoader (). getResourceAsStream (name);    New Properties ();   P.load (in);

6. Examples of Getsystemresourceasstream () static methods using the Java.lang.ClassLoader class :

InputStream in = classloader.getsystemresourceasstream (name);    New Properties ();   P.load (in);   

7. The Javax.servlet.ServletContext getResourceAsStream () method can be used in the servlet example:

InputStream in = context.getresourceasstream (path);    New Properties ();   P.load (in);   

Java Read properties configuration file [Go]

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.