Read Data from a Properties File in Java



Java supports file-handling; it provides various classes that provide various methods to read, write, update, and delete data from files in our local system.

A properties file is a simple text file with a ".properties" extension that contains configuration data in the form of key-value pairs. It is mostly used in Java applications to manage settings such as database configuration, application messages, or environment variables.

How to read Data from a Properties File in Java?

To read data from the properties file, you can use the Properties class in Java. This is a subclass of the Hashtable class and it represents a persistent set of properties.

The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list a string.

The Properties.load() method of the Properties class loads the ".properties" file in the form of key-value pairs.

Syntax

Following is the syntax to define the Properties class in Java:

public class Properties extends Hashtable

Let's create a file with a .properties extension in our local system and use the Property class to read data from it.

The following is the properties file named credentials.properties from which the data will be read:

Properties File

Example 1

The following program uses file handling to read the data from the properties file named credentials.properties file:

import java.io.*;
import java.util.*;
public class ReadPropertiesFileTest {
   public static void main(String args[]) throws IOException {
      Properties prop = readPropertiesFile("credentials.properties");
      System.out.println("Username: "+ prop.getProperty("username"));
      System.out.println("Password: "+ prop.getProperty("password"));
   }
   public static Properties readPropertiesFile(String fileName) throws IOException {
      FileInputStream fis = null;
      Properties prop = null;
      try {
         fis = new FileInputStream(fileName);
         prop = new Properties();
         prop.load(fis);
      } catch(FileNotFoundException fnfe) {
         fnfe.printStackTrace();
      } catch(IOException ioe) {
         ioe.printStackTrace();
      } finally {
         fis.close();
      }
      return prop;
   }
}

The above program produces the following output:

Username: admin
Password: admin@123

The following is the properties file named users.properties from which the data will be read:

Properties File

Example 2

The following is another example of reading the data from a properties file named users.properties using the file-handling:

import java.io.*;
import java.util.*;
public class ReadPropertiesFileTest {
   public static void main(String args[]) throws IOException {
      Properties prop = readPropertiesFile("users.properties");
      System.out.println("Name: "+ prop.getProperty("name"));
      System.out.println("Age: "+ prop.getProperty("age"));
	  System.out.println("City: "+ prop.getProperty("city"));
	  System.out.println("Email: "+ prop.getProperty("email"));
   }
   public static Properties readPropertiesFile(String fileName) throws IOException {
      FileInputStream fis = null;
      Properties prop = null;
      try {
         fis = new FileInputStream(fileName);
         prop = new Properties();
         prop.load(fis);
      } catch(FileNotFoundException fnfe) {
         fnfe.printStackTrace();
      } catch(IOException ioe) {
         ioe.printStackTrace();
      } finally {
         fis.close();
      }
      return prop;
   }
}

The above program produces the following output:

Name: "Rohan Verma"
Age: 30
City: "Lucknow"
Email: "[email protected]"
Updated on: 2025-06-23T11:31:51+05:30

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements