Java – Loading Property File

The properties object contains key and value pair both as a string. The java.util.Properties class is the subclass of
Hashtable.
It can be used to get property value based on the property key. The Properties class provides methods to get data
from properties file and store data into properties file.

Use following sample applicaiton to load a properties file bundled :

 
package com.pwn.property;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class LoadingProperty {
	
	/*user.properties
	name=Naveen
	country=US*/

	public static void main(String[] args) {
		try (InputStream bundledResource = LoadingProperty.class
				.getResourceAsStream("user.properties")) {
			Properties userProperties = new Properties();
			userProperties.load(bundledResource);
			System.out.println("username from property file "
					+ userProperties.get("name"));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
username from property file Naveen

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s