Several ways to load properties in Java

Source: Internet
Author: User

1. Use the load () method of the Java.util.Properties class (note: jdbc.properties If this file is loaded in this way, it must be placed under the classpath or it will not load)

        InputStream InputStream = new Bufferedinputstream (New   FileInputStream (New File ("Jdbc.properties"));        Properties Properties =new properties ();        Properties.load (InputStream);        System.out.println (Properties.get ("Jdbc.url"));

2. Use the Getbundle () method of the Java.util.ResourceBundle class (note: The file's write does not have a suffix name for the file )

            ResourceBundle RB = Resourcebundle.getbundle ("jdbc", Locale.getdefault ());            enumeration<string> keys = Rb.getkeys ();            while (Keys.hasmoreelements ()) {                  string key = (string) keys.nextelement ();                  System.out.println (rb.getstring (key));           }

3. Use the constructor of the Java.util.PropertyResourceBundle class

        InputStream InputStream = new Bufferedinputstream (New FileInputStream ("Src/jdbc.properties"));        ResourceBundle bundle =new propertyResourceBundle (inputstream);        System.out.println (bundle.getstring ("Jdbc.url"));
4. Using the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class
         InputStream InputStream = Classloader.getsystemresourceasstream ("jdbc.properties");          Properties Properties =new properties ();         Properties.load (InputStream);         System.out.println (Properties.get ("Jdbc.url"));
5. getResourceAsStream () method of Java.lang.ClassLoader obtained using Class.getclassloader ()
         InputStream InputStream = JDBCProperties.class.getClassLoader (). getResourceAsStream ("Jdbc.properties");         Properties Properties =new properties ();         Properties.load (InputStream);         System.out.println (Properties.get ("Jdbc.url"));

6. Using the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class

         InputStream InputStream = JDBCProperties.class.getResourceAsStream ("/jdbc.properties");         Properties Properties =new properties ();         Properties.load (InputStream);         System.out.println (Properties.get ("Jdbc.url"));

7. The Javax.servlet.ServletContext getResourceAsStream () method can be used in a servlet

       InputStream in = context.getresourceasstream (path);       Properties P = new properties ();       P.load (in);

  

Several ways to load properties in Java

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.