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]