• To store configurable parameters, .properties file will be used in java.
  • We can Store data in key value pair (key=value).
  • Re compilation not required if we change any keys or values in properties file.
  • Used for internationalization  and to store frequently changeable values. 
  • java.util.Properties class is sub class of Hashtable. 
  • We can read / load the propertied file using InputStream.
  • InputStream inputStream = getClass().getClassLoader().getResourceAsStream(properties_FileName);
  • Create a maven project, under resources create one .properties file and load this file in main class using getClass().getClassLoader().getResourceAsStream(properties_FileName).
  • Lets see an example java program on java read properties file from resource folder or how to read values from properties file in java example or how to get values from properties file in java.

 #1: Create a maven project:

java read properties file from classpath


Create properties file:

reading properties file in java

#1: Java example program  to get values from properties file in java

  1. package com.instanceofjava.propertiesfile;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Date;
  6. import java.util.Properties;
  7. /**
  8.  *  
  9. * @author www.instanceofjava.com
  10.  * @category: java example programs
  11.  *  
  12. * Write a java example program to read/ load properties file
  13.  *
  14.  */
  15. public class ReadPropertiesFile {
  16.  
  17.     public Properties getProperties() throws IOException {
  18.         
  19.         InputStream inputStream=null;
  20.         Properties properties = new Properties();
  21.         try {
  22.             
  23.             String propFileName = "config.properties";
  24.  
  25.             inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
  26.  
  27.             if (inputStream != null) {
  28.                 properties.load(inputStream);
  29.             } else {
  30.                 throw new FileNotFoundException("property file '" + propFileName + "' not found
  31. in the classpath");
  32.             }
  33.  
  34.  
  35.         } catch (Exception e) {
  36.             System.out.println("Exception: " + e);
  37.        } finally {
  38.             inputStream.close();
  39.         }
  40.          return properties;
  41.     }
  42.     
  43.     
  44.     public static void main(String[] args) throws IOException {
  45.         
  46.         ReadPropertiesFile obj= new ReadPropertiesFile();
  47.         Properties properties=obj.getProperties();
  48.         // get the each property value using getProperty() method 
  49.         System.out.println("username: "+properties.getProperty("username"));
  50.         System.out.println("password: "+properties.getProperty("password"));
  51.         System.out.println("url: "+properties.getProperty("url"));
  52.         
  53.                 
  54.     }
  55.  
  56. }

Output:

  1. username: user1
  2. password: abc123
  3. url: www.instanceofjava.com

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu