Java reads the properties file and java reads properties

Source: Internet
Author: User

Java reads the properties file and java reads properties

In actual development, we will put some path file configurations in the properties file, so we only need to write this configuration file when modifying the path, you do not need to modify the code one by one. But how can we get the values in the configuration file? In fact, this is very simple. We only need to encapsulate the following tool class:

Public class UrlUtil {private static Properties config = null; static {InputStream in = UrlUtil. class. getClassLoader (). getResourceAsStream ("url. properties "); config = new Properties (); try {config. load (in); in. close ();} catch (IOException e) {// read the url. properties error e. printStackTrace () ;}}/*** obtain the corresponding value through the key * @ param key * @ return */public static String getValue (String key) {try {String value = config. getProperty (key); return value. trim ();} catch (Exception e) {e. printStackTrace (); return null ;}/ *** read all properties information */public static void getAllProperties () {try {Enumeration en = config. propertyNames (); while (en. hasMoreElements () {String key = (String) en. nextElement (); String Property = config. getProperty (key) ;}} catch (Exception e) {e. printStackTrace (); System. err. println ("ConfigInfoError" + e. toString () ;}} public static void main (String args []) {}}

The url. properties in the Code is the configuration file of our placement path. For example, there is an image path in our url. properties: imgPath = c:/images

To obtain this value, you only need to use UrlUtil. getValue ("imgPath.

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.