Good code is its own best documentation. As you're about to add a comment, ask yourself, 'How can I improve the code so that this comment isn't needed?' Improve the code and then document it to make it even clearer. Steve McConnell

Properties

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

0 Comments


Definition

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.

Character

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) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	 
		}
		
		
		Properties defaultProps = new Properties();
		FileInputStream in;
		try {
			in = new FileInputStream("search.properties");
			System.out.println("\nLoad 'dbconfig.properties' properties");
			defaultProps.load(in);
			
			in.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}
		

		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"));
		
		
		

	}

}
						

Output

    					 
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
    					 
					    

Comparison

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.

0 Comments



Please login to add comments.