Java read properties file


 

Java read properties file

In this section, you will learn how to read properties file.

In this section, you will learn how to read properties file.

Java read properties file

In this section, you will learn how to read properties file.

Description of code:

There are different tools to access different files. Now to load the data from the properties file, we have used Properties class. This class allowed us to save data to a stream or loaded from a stream using the properties file. In the properties file, data has been stored as a key value pair in the form of string.

load()- This method reads a property list (key and element pairs) from the input stream.

getProperty() - This method searches for the property with the specified key in the properties file.

Here is the data.properties:

name=RoseIndia
address=Rohini, Delhi

Video tutorial of reading properties file in Java: "How to read properties file in Java?"

Here is the code:

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

public class ReadPropertiesFile {
	public static void main(String[] args) {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream("C:/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) {
		}
	}
}

Through the above code, you can read any properties file.

Output:

Name: RoseIndia
Address: Rohini, Delhi

Ads