Home Tutorial Java Core Files Java read properties file

 
 

Java read properties file
Posted on: July 4, 2006 at 12:00 AM
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

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

Related Tags for Java read properties file:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.