Research on Using Java to read the properties configuration file

Source: Internet
Author: User

Package COM. anhang. util; </P> <p> Import Java. util. properties; </P> <p> Import Java. io. inputstream; <br/> Import Java. io. ioexception; </P> <p> public class datasource {</P> <p> Public final static string driver; <br/> Public final static string username; <br/> Public final static string password; <br/> Public final static string pagesize; </P> <p> static {<br/> inputstream in = datasource. class. getclassloader (). getresourceasstream (<br/> "config. properties "); <br/> properties P = new properties (); <br/> try {<br/> P. load (in); <br/>}< br/> catch (ioexception IE) {<br/> IE. printstacktrace (); <br/>}< br/> system. out. println ("initialize Database Configuration File"); <br/> driver = P. getproperty ("driver"); <br/> system. out. println ("Driver:" + driver); <br/> username = P. getproperty ("username"); <br/> system. out. println ("username:" + username); <br/> Password = P. getproperty ("password"); <br/> system. out. println ("Password:" + password); <br/> pagesize = P. getproperty ("pagesize"); <br/> system. out. println ("pagesize:" + pagesize); <br/>}< br/>

The above is JavaCode

 

 

Configuration file config. Properties

Put it in the src directory

 

The format is as follows:

 

# Database-driven
Drivers = 123

# Username and password
Username = db2admin
Password = 123456

# Number of entries per page
Pagesize = 20

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.