JAVA methods for reading attribute files

Source: Internet
Author: User

1. Use the load () method of java. util. Properties class

Example:

Java code
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:

Java code
ResourceBundle rb = ResourceBundle. getBundle (name, Locale. getDefault ());
3. Use the constructor of the java. util. PropertyResourceBundle class
Example:

Java code
InputStream in = new BufferedInputStream (new FileInputStream (name ));
ResourceBundle rb = new PropertyResourceBundle (in );
4. Use the getResourceAsStream () method of the class variable
Example:

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

Java code
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:


Java code
InputStream in = ClassLoader. getSystemResourceAsStream (name );
Properties p = new Properties ();
P. load (in );
7. Use the PropertiesConfiguration class of apache

Example:
Java code

Configuration config = new PropertiesConfiguration ("test. properties ");

Config. getProperty (key );

 

Supplement

You can use the getResourceAsStream () method of javax. Servlet. ServletContext in servlet.

Example:

Java code
InputStream in = context. getResourceAsStream (path );
Properties p = new Properties ();
P. load (in );

Here, the name is the name of the properties file. However, I found someone on the internet saying that they want to write the absolute path of the properties file; otherwise, the test fails. I have not verified it. If you are interested, try it.
Personally, I prefer to use the 3rd method. I found a more detailed article on the Internet. The full text is as follows:
During the design, we often need to access some configuration information suitable for local modification. If it is used as a static variable, a class needs to be re-compiled for each modification ,. config is not suitable for storing such information. In this case, we need ResourceBundle.
Through ResourceBundle, We need to access a text type file with the suffix properties located under the/WEB-INF/classes directory to read the value we need from it.

Java code
Locale locale = Locale. getDefault ();
ResourceBundle localResource = ResourceBundle. getBundle ("ConnResource", locale );

String value = localResource. getString ("test ");
System. out. println ("ResourceBundle:" + value );
The content of the/WEB-INF/class/ConnResource. properties file is as follows:

Test = hello world
The printed result is hello world.
Note that we can use the combination of Locale and ResourceBundle to create an international java program. We can instantiate locale

Java code
New Locale ("zh", "CN ");
Pass

Java code
ResourceBundle. getBundle ("MessagesBundle", locale );

The system will automatically search for MessagesBundle_zh_CN, which is defined as simplified Chinese in mainland China. If the file does not exist, MessagesBundle_zh and MessagesBundle will be searched in sequence until they are found.

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.