Read the configuration file in Java

Source: Internet
Author: User

If a Java Web project is running on a tomcat or other container, you can use the following method to get the input stream of the file

1. When the attribute file is placed under SRC

InputStream is = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Test.properties");  Properties P = new properties ();  P.load (IS);  

2. When a property file is placed under a package, such as: Com.test.config

InputStream is = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("Test.properties");  Properties P = new properties ();  P.load (IS);  

For a Java project, use the following method to read the properties file

InputStream is = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("com/test/config/ Test.properties ");  Properties P = new properties ();  P.load (IS);  

If the property file is placed under a package, use Com/test/config/test.properties to

*.properties format of the file in Java or quite a lot of, can be said to write Java project as long as the configuration data will basically use *.properties to do, this is the official Java support of a configuration file, JDK provides a ready-made API to operate.

When you use the properties format file in Eclipse, by default the Chinese in the properties file is converted to ASC, and we can install a properties Editor plugin for Eclipse to support the Chinese display.

Address: http://blog.csdn.net/abc_key/article/details/46511259;

Read the configuration file in Java

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.