Wednesday, April 14, 2021

How to Read Properties File in Java

In this tutorial you will see how to read a properties file in Java. If you have any configurable data in your application like DB configuration, user settings its better to keep it in a properties file and read it from there. A properties file stores data in the form of key/value pair.

For reading a properties file in Java there are two ways to load properties file-

  1. Loading properties file from the file system. See example.
  2. Loading properties file from classpath. See example.

Project structure

For this example we’ll have a properties file named app.properties file in a folder called resource. The resource folder is at the same level at the src folder in the Java project.

read properties file in java

Steps for reading a properties file in Java

  1. Create an instance of Properties class.
  2. Create a FileInputStream by opening a connection to the properties file.
  3. Read property list (key and element pairs) from the input stream using load() method of the Properties class.

Content of the properties file

Here the properties file used is named app.properties file with it’s content as-

user=TestUser
url=https://www.netjstech.com

Loading properties file from the file system

One way to read properties file in Java is to load it from the file system.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropDemo {
  private Properties properties = new Properties();
  public static void main(String[] args) {
    PropDemo pDemo = new PropDemo();
    pDemo.loadPropertiesFile();
    pDemo.readProperties();
  }
  
  // This method is used to load the properties file
  private void loadPropertiesFile(){
    InputStream iStream = null;
      try {
        // Loading properties file from the path (relative path given here)
        iStream = new FileInputStream("resource/app.properties");   
        properties.load(iStream);
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }finally {
        try {
          if(iStream != null){
            iStream.close();
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }
    }
  
  /**
  * Method to read the properties from a
  * loaded property file
  */
  private void readProperties(){
    System.out.println("User name - " + properties.getProperty("user"));
    System.out.println("URL - " + properties.getProperty("url"));
    // reading property which is not there
    System.out.println("City - " + properties.getProperty("city"));
  }
}

Output

User name - TestUser
URL - https://www.netjstech.com
City - null

Here you can see that in the code there is an attempt to read the property “city” which doesn’t exist in the app.properties file that’s why it is retrieved as null.

Loading properties file from classpath

If you have properties file in the project classpath then you can load it by using the getResourceAsStream method. That is another way to read properties file in Java.

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropDemo {
 private Properties properties = new Properties();
 public static void main(String[] args) {
   PropDemo pDemo = new PropDemo();
   pDemo.loadProperties();
   pDemo.readProperties();
 }
 
 // This method is used to load the properties file
 private void loadProperties(){
   InputStream iStream = null;
   try {
    // Loading properties file from the classpath
    iStream = this.getClass().getClassLoader()
                             .getResourceAsStream("app.properties");
    if(iStream == null){
     throw new IOException("File not found");
    }
    properties.load(iStream);
   } catch (IOException e) {
    e.printStackTrace();
   }finally {
    try {
     if(iStream != null){
      iStream.close();
     }
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
 }
  
 /**
  * Method to read the properties from a
  * loaded property file
 */
 private void readProperties(){
   System.out.println("User name - " + properties.getProperty("user"));
   System.out.println("URL - " + properties.getProperty("url"));
 }
}

Output

User name - TestUser
URL - https://www.netjstech.com

That's all for this topic How to Read Properties File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Java Programs Page


Related Topics

  1. How to Read File From The Last Line in Java
  2. Unzip File in Java
  3. Reading File in Java Using Files.lines And Files.newBufferedReader
  4. Write to a File in Java
  5. How to Read Properties File in Spring Framework

You may also like-

  1. How to Create PDF From XML Using Apache FOP
  2. Setting And Getting Thread Name And Thread ID in Java
  3. How to Convert Date And Time Between Different Time-Zones in Java
  4. How to Reverse a String in Java
  5. Fail-Fast Vs Fail-Safe Iterator in Java
  6. Java ReentrantReadWriteLock With Examples
  7. BigDecimal in Java With Examples
  8. instanceof Operator in Java With Examples