Use the apache commons package to read the configuration file

Source: Internet
Author: User

In Java projects, you often need to read configuration information from the configuration file. Common configuration files include properties files and XML files. This document takes reading the properties configuration file as an example.

The screenshot of the configuration file app. properties is as follows:

ftp.username=userOneftp.password=p@sswordftp.hostip=127.0.0.1ftp.port=21

Load and read the Configuration:

Import org. apache. commons. configuration. configurationException; import org. apache. commons. configuration. propertiesConfiguration; import org. apache. commons. configuration. reloading. fileChangedReloadingStrategy; private static String ftpUser = null; private static String ftpPassword = null; private static String ftpHost = null; private static String ftpPort = null; private static String CONFIG_FILEPATH = ClassLoader. getSystemResource ("app. properties "). getPath (); private static void initFromProperties () {try {CONFIG_FILEPATH = URLDecoder. decode (CONFIG_FILEPATH, "UTF-8"); setProperties (new PropertiesConfiguration (CONFIG_FILEPATH); getProperties (). setReloadingStrategy (new FileChangedReloadingStrategy (); getProperties (). setAutoSave (true); readValues ();} catch (UnsupportedEncodingException e) {// handle exception} catch (ConfigurationException e) {// handle exception} private static void readValues () {// ftp setFtpUser (getStrValue ("ftp. username "); setFtpPassword (getStrValue (" ftp. password "); setFtpHost (getStrValue (" ftp. hostip "); setFtpRootPath (getStrValue (" ftp. ftproot "); setFtpPort (getStrValue (" ftp. port "));}

You can also use the Apache commons package for the xml configuration file.

Heavy rain: [http://www.blogjava.net/cmscer/archive/2013/05/22/399633.html]

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.