Share on Google+Share on Google+

Java read properties file

Advertisement
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

Advertisements

Advertisement

Posted on: July 4, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.