J2SE API: Six Methods to read Properties files

Source: Internet
Author: User

Source: http://www.study-code.com/java/j2se/66421.htm

// Use J2SEAPI to read Properties files

// 1. Example of the load () method using the java. util. Properties class:
InputStreamin = lnewBufferedInputStream (newFileInputStream (name ));
Propertiesp = newProperties ();
P. load (in );

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

// 3. Example of a constructor using the java. util. PropertyResourceBundle class:
InputStreamin = newBufferedInputStream (newFileInputStream (name ));
ResourceBundlerb = newPropertyResourceBundle (in );

// 4. Example of the getResourceAsStream () method using the class variable:
InputStreamin = JProperties. class. getResourceAsStream (name );
Propertiesp = newProperties ();
P. load (in );

// 5. Example of the getResourceAsStream () method of java. lang. ClassLoader obtained by using class. getClassLoader:
InputStreamin = JProperties. class. getClassLoader (). getResourceAsStream (name );
Propertiesp = newProperties ();
P. load (in );
// 6. Example of using the getSystemResourceAsStream () Static Method of the java. lang. ClassLoader class:
InputStreamin = ClassLoader. getSystemResourceAsStream (name );
Propertiesp = newProperties ();
P. load (in );

// Supplement
// The getResourceAsStream () method of javax. Servlet. ServletContext can be used in servlet as an example:
InputStreamin = context. getResourceAsStream (path );
Propertiesp = newProperties ();
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.