J2SE API: Six Methods to read Properties files

Source: Internet
Author: User

Use J2SE API to read Properties files

1. Use the load () method of java. util. Properties class
Example: InputStream in = lnew BufferedInputStream (new FileInputStream (name ));
Properties p = new Properties ();
P. load (in );

2. Use the getBundle () method of the java. util. ResourceBundle class
Example: ResourceBundle rb = ResourceBundle. getBundle (name, Locale. getDefault ());

3. Use the constructor of the java. util. PropertyResourceBundle class
Example: InputStream in = new BufferedInputStream (new FileInputStream (name ));
ResourceBundle rb = new PropertyResourceBundle (in );

4. Use the getResourceAsStream () method of the class variable
Example: InputStream in = JProperties. class. getResourceAsStream (name );
Properties p = new Properties ();
P. load (in );

5. GetResourceAsStream () method of java. lang. ClassLoader obtained by using class. getClassLoader ()
Example: InputStream in = JProperties. class. getClassLoader (). getResourceAsStream (name );
Properties p = new Properties ();
P. load (in );

6. Use the getSystemResourceAsStream () Static Method of the java. lang. ClassLoader class
Example: InputStream in = ClassLoader. getSystemResourceAsStream (name );
Properties p = new Properties ();
P. load (in );

Supplement

You can use the getResourceAsStream () method of javax. Servlet. ServletContext in servlet.
Example: InputStream in = context. getResourceAsStream (path );
Properties p = new Properties ();
P. load (in );

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.