Why do we never have time to do it right, but always have time to do it over?. Anonymous


Language Java | Level Intermediate | Category Java collections framework (JCF) | 2015-07-22 23:29:23



Properties are configuration values managed as key/value pairs. The keys are not duplicated and used for retrieving the values from properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.


Properties can save to the file and load from the file.
The properties file can create another properties.
It also supports load from XML format properties. The XML file can change the encoding.
The Properties derived from Hashtable.

source Code

package com.careerdrill.collections.map;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

public class PropertiesUse {

	public static void main(String[] args) {
		Properties prop = new Properties();
		OutputStream output = null;
		try {
	 System.out.println("Create search.properties file");
			output = new FileOutputStream("search.properties");
			System.out.println("set 'url' property as 'http://www.google.com'");
			prop.setProperty("url", "http://www.google.com");
			System.out.println("set 'keywords' property as 'careerdrill'");
			prop.setProperty("keywords", "careerdrill");
			prop.store(output, null);
		} catch (IOException io) {
		} finally {
			if (output != null) {
				try {
				} catch (IOException e) {
		Properties defaultProps = new Properties();
		FileInputStream in;
		try {
			in = new FileInputStream("search.properties");
			System.out.println("\nLoad 'dbconfig.properties' properties");

		} catch (FileNotFoundException e) {
		}catch(IOException e){

		Properties applicationProps = new Properties(defaultProps);

		System.out.println("Get 'url' property from config file: "+ applicationProps.getProperty("url"));
		System.out.println("Get 'keywords' property from config file: "+ applicationProps.getProperty("keywords"));




Create search.properties file
set 'url' property as 'http://www.google.com'
set 'keywords' property as 'careerdrill'
Load 'dbconfig.properties' properties
Get 'url' property from config file: http://www.google.com
Get 'keywords' property from config file: careerdrill


Properties Hash map
Thread-safe: multiple threads can share a single Properties object. Non-synchronized and not thread safe.
Do not allow null keys and null values. Hashmap allows one null key and any number of null values.
Not fail-fast iterator. Uses fail-fast iterator.
Hashtable slower than hash map due to thread synchronization support. Faster compare than Hashtable.
Hashtable derived from Dictionary which is obsolete now. Hash map extended from AbstractMap class.

When to use

When your program requires some configuration parameter which will change between the environments, you can use Properties. The application can load the existing properties or create the new properties.


Please login to add comments.