Read the properties file with Java — transfer to

Today, Google took a long time to read the properties file through Java and finally found it. Now I'd like to share it with you. The following code is posted directly: Java class

Method 2:

Transferred from: http://duqiangcise.iteye.com/blog/319793

There are six ways to read the properties file using J2SE API 1. Using Java util. Example of load() method of properties class: InputStream in = lnew bufferedinputstream (New FileInputStream (name)); Properties p = new Properties(); p.load(in); 2。 Using Java util. Example of getbundle () method of resourcebundle class: resourcebundle RB = resourcebundle getBundle(name,Locale.getDefault()); 3。 Using Java util. Constructor example of propertyresourcebundle class: InputStream in = new bufferedinputstream (New FileInputStream (name)); ResourceBundle rb = new PropertyResourceBundle(in); 4。 Example of getresourceasstream() method using class variable: InputStream in = jproperties class. getResourceAsStream(name); Properties p = new Properties(); p.load(in); 5。 Use class Getclassloader() Example of getresourceasstream() method of lang. classloader: InputStream in = jproperties class. getClassLoader(). getResourceAsStream(name); Properties p = new Properties(); p.load(in); 6。 Using Java Example of getsystemresourceasstream() static method of lang.classloader class: InputStream in = classloader getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); Javax can be used in supplementary servlets servlet. Example of getresourceasstream() method of ServletContext: InputStream in = context getResourceAsStream(path); Properties p = new Properties(); p.load(in);

be careful:

this. getclass. getResourceAsStream(name); // Class is at the same level as the resource file -- in the same directory

this. class. getClassLoader(). getResourceAsStream(name); // The resource file is at the same level as classpath.

Unknown source

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>