How to read properties file in Java?

In this tutorial you will learn "How to read properties file in Java?". The data of the property file is displayed on the console.

How to read properties file in Java?

In this tutorial you will learn "How to read properties file in Java?". The data of the property file is displayed on the console.

How to read properties file in Java?

Example program of reading a properties file and then printing the data on console

In this video tutorial I will show you how to use the java.util.Properties class for reading a property file in Java program

The java.util.Properties class is subclass of the Hashtable, which is used to map the keys to value.

The java.util.Properties class is used to read the properties file. The properties files are simple text file which is saved as .properties extensions.

Here is example of property file:

name=Rose India
address=Rohini, Delhi

Properties file contains data in the key and value pair.

The getProperty() method of the Properties class is used to read the value of the properties file.

Here is the video tutorial of reading a property file in Java: "How to read properties file in Java?"

Here is the data.properties the properties file used in this example:

name=Rose India
address=Rohini, Delhi

Here is the source code of the Java program(ReadPropertiesFile.java):

package net.roseindia;

import java.io.*;
import java.util.*;

public class ReadPropertiesFile {
	public static void main(String[] args) {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream("./data.properties"));
			String name = prop.getProperty("name");
			String address = prop.getProperty("address");
			System.out.println("Name: " + name);
			System.out.println("Address: " + address);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

Above program displays the value of the keys "name" and "address" on the console.

You can find more tutorials of Java Properties class in our Java properties files tutorials section.