Java Read the properties file
Six ways to read property files common in the network
If the configuration file is in the package, the path to the configuration file is typically found under classpath, such as conf/crm/ Db.properties, the default path under Classpath, if the configuration file is under Classpath, is not placed in the package, write the file name directly.
1. Using the Load () method of the Java.util.Properties class
Example: InputStream in = Lnew bufferedinputstream (new FileInputStream (name));
Properties P = new properties ();
P.load (in);
2. Using the Getbundle () method of the Java.util.ResourceBundle class
Example: ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
3. Use the constructor of the Java.util.PropertyResourceBundle class
Example: InputStream in = new Bufferedinputstream (new FileInputStream (name));
ResourceBundle RB = new propertyResourceBundle (in);
4. The getResourceAsStream () method using the class variable
Example: InputStream in = JProperties.class.getResourceAsStream (name);
Properties P = new properties ();
P.load (in);
5. The getResourceAsStream () method of Java.lang.ClassLoader obtained using Class.getclassloader ()
Example: InputStream in = JProperties.class.getClassLoader (). getResourceAsStream (name);
Properties P = new properties ();
P.load (in); If the configuration file is in the package, the path to the configuration file to be added is typically found under classpath. such as Conf/crm/db.properties
6. Using the Getsystemresourceasstream () static method of the Java.lang.ClassLoader class
Example: InputStream in = Classloader.getsystemresourceasstream (name);
Properties P = new properties ();
P.load (in);
Add
The getResourceAsStream () method of Javax.servlet.ServletContext can be used in Servlets
Example: InputStream in = context.getresourceasstream (path);
Properties P = new properties ();
P.load (in);
Most of the actual usage is Class.getresourceasstream (name)
When used, it is best to use the specified class name, or use the object directly
Object.class.getResourceAsStream ("path")
Reference: http://lavasoft.blog.51cto.com/62575/62174;http://blog.csdn.net/xue_feitian/article/details/6750618
Java Read the properties file