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