Summary of several ways to read properties files in JAVA

  • 2020-04-01 02:23:49
  • OfStack

1. Use the load() method of the java.util.properties class

Example:

Java code
InputStream in = lnew BufferedInputStream(new FileInputStream(name));    
  Properties p = new Properties();    
  P.l oad (in);  

2. Use the getBundle() method of the java.util.ResourceBundle class  

Example:

Java code
ResourceBundle rb = resourcebund.getbundle (name, locales. GetDefault ()));  

3. Using Java. Util. PropertyResourceBundle class constructor  

Example:

Java code
InputStream in = new BufferedInputStream(new FileInputStream(name));    
  ResourceBundle rb = new PropertyResourceBundle(in);  

4. The getResourceAsStream() method & strapped using the class variable;

Example:

Java code
InputStream in = JProperties. Class. GetResourceAsStream (name);    
  Properties p = new Properties();    
  P.l oad (in);  

5. GetResourceAsStream () method of java.lang.ClassLoader obtained by using ClassLoader()  

Example:

Java code
InputStream in = JProperties. Class. GetClassLoader () getResourceAsStream (name);    
  Properties p = new Properties();    
  P.l oad (in);  

6. Using Java. Lang. This class of getSystemResourceAsStream () static method  

Example:

Java code
InputStream in = this. GetSystemResourceAsStream (name);    
  Properties p = new Properties();    
  P.l oad (in);  

7. Use apache's PropertiesConfiguration class

Example:
Java code
Configuration config = new PropertiesConfiguration("test.properties");
The config. GetProperty (key);


Related articles: