Read the properties configuration file in JAVA Singleton Mode

Source: Internet
Author: User

Some time ago, I went on a business trip to do a data interface project, there are a lot of parameters need to be read from the configuration file system-config.proterties file in the project, almost every class needs to be used, therefore, you should read all the configuration items in the configuration file and store them in a Map. The first thought is the singleton mode. This saves resources. After searching for some information, I started to write it and pasted the code: Java code: import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java. util. enumeration; import java. util. hashMap; import java. util. map; import java. util. properties; public class SystemConfigUtil {private static SystemConfigUtil systemConfigUtil = null; private static Map <String, String> proMap = new HashMap <String, S Tring> (); private SystemConfigUtil () {File file = new File ("src \ system-config.properties"); InputStream is = null; Properties pro = new Properties (); try {is = new FileInputStream (file); pro. load (is); Enumeration e = pro. propertyNames (); while (e. hasMoreElements () {String key = (String) e. nextElement (); String value = (String) pro. get (key); proMap. put (key, value) ;}} catch (IOException e ){ E. printStackTrace () ;}finally {try {is. close (); // do not forget to close the resource!} Catch (IOException e) {e. printStackTrace () ;}} public static SystemConfigUtil getInstance () {if (systemConfigUtil = null) {systemConfigUtil = new SystemConfigUtil ();} return null;} public static Map <String, string> getProMap () {return proMap;} public static void main (String [] args) {// write a main method to verify SystemConfigUtil d1 = SystemConfigUtil. getInstance (); SystemConfigUtil d2 = SystemCo NfigUtil. getInstance (); System. out. println (d1 = d2); Map m1 = d1.getProMap (); Map m2 = d2.getProMap (); System. out. println (m1 = m2); System. out. println (m1.get ("ip") ;}} because it is used in a project, the configuration file is directly written to the program. A slight change can become dynamic. After testing, it takes only 2 milliseconds to read a configuration file (5 K) (on the top of my machine configuration ).

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.