Read configuration file in Java (Properties, XML)

Source: Internet
Author: User

1. Using the tool class properties provided by Java.util
-First, I have a file.properties file on my side.
-then go to read this file



Note Project Path


//注意ProConfigUtil是当前类,也可用this替代    InputStream inputStream = PropConfigUtil.class.getClassLoader()           .getResourceAsStream("config/file.properties");     Properties pro = new Properties();    pro.load(inputStream);           String fileType = pro.getProperty("file.fileType").trim();



Full code

 PackageCom.zsl.uploadfile;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.util.Properties;ImportOrg.apache.commons.configuration.ConfigurationException; Public Final  class imagepropconfigutil{     Public Imagepropconfigutil()    {} Public Static  class SingleTon {        Private StaticInputStream InputStream =NULL;Private StaticProperties Pro =NULL;Static{InputStream = ImagePropConfigUtil.class.getClassLoader (). getResourceAsStream ("Config/file.properties"); Pro =NewProperties ();Try{pro.load (InputStream); }Catch(IOException e)            {E.printstacktrace (); }        }    } Public StaticStringGetval(String key) {returnSingleTon.pro.getProperty (key); } Public Static void Main(string[] args)throwsConfigurationException {System.out.println ("--------------------"); String val = imagepropconfigutil.getval ("File.filetype"); System.out.println ("-------file type: \ t"+ val); }}



2. Using the jar package: Apache-commons-configuration-1.5.jar

PropertiesConfiguration prop = new PropertiesConfiguration("config/file.properties");prop.setDelimiterParsingDisabled(true);String val = prop.getProperty("image.imageType").toString();System.out.println(val);


Full code

Package Com.zsl.uploadfile;import Org.apache.commons.configuration.configurationexception;import Org.apache.commons.configuration.PropertiesConfiguration; Public classutil{ Public Util()    {} Public Static classSingle {Private StaticPropertiesconfiguration prop =NULL;Static{Try{prop =NewPropertiesconfiguration ("Config/file.properties"); Prop.setdelimiterparsingdisabled (true); }Catch(ConfigurationException e) {System. out. println (E.getmessage ());            E.printstacktrace (); }        }    } Public StaticStringGetval(String key) {Stringvalue= Single.prop.getProperty (key). ToString ();return value; } Public Static void Main(string[] args) {System. out. println (Util.getval ("File.filetype")); }}



Both of these methods can be used to read the configuration file. Compared with the more efficient reading configuration file, simulating a singleton mode, the program is only called once,

The printing results are as follows:








Read configuration file in Java (Properties, XML)

Related Article

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.