Several common ways to read. properties files in online articles
1. Example of the load () method using the Java.util.Properties class :
InputStream in = Lnew bufferedinputstream (new FileInputStream (name)); New Properties ();
2. Using the Getbundle () method of the Java.util.ResourceBundle class
ResourceBundle RB = Resourcebundle.getbundle (name, Locale.getdefault ());
Use ResourceBundle to read. properties files to avoid path problems
When I read the. properties file in the jar, I always can't find the file path and later read the. properties file with ResourceBundle to avoid the path problem, the code is as follows:
Process is the file name, remember not to add. Properties, URL is the key name in the file
ResourceBundle bundle = Resourcebundle.getbundle ("com.ihandy.smsoc.app.process"); = Bundle.getstring ("URL"); System.out.println (s); = S;
3. Use the constructor of the Java.util.PropertyResourceBundle class
New Bufferedinputstream (new FileInputStream (name)); New propertyResourceBundle (in);
4. The getResourceAsStream () method using class variables
InputStream in = class name. class . getResourceAsStream (name); New Properties (); P.load (in);
5. Example of the Java.lang.ClassLoader getResourceAsStream () method obtained using Class.getclassloader () :
InputStream in = class name. class . getClassLoader (). getResourceAsStream (name); New Properties (); P.load (in);
6. Examples of Getsystemresourceasstream () static methods using the Java.lang.ClassLoader class :
InputStream in = classloader.getsystemresourceasstream (name); New Properties (); P.load (in);
7. The Javax.servlet.ServletContext getResourceAsStream () method can be used in the servlet example:
InputStream in = context.getresourceasstream (path); New Properties (); P.load (in);
Java Read properties configuration file [Go]