Properties

java.util.Properties Example

In this example we will show how to use java.util.Properties class. Properties class is a subclass of Hashtable and represents a persistent set of properties. The Properties are key/value pairs that can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. The Properties class offers you the capability to specify a default property that will be returned if no value is associated with a certain key.

1. Class Properties constructors

The java.util.Properties class supports two constructors:
 

  • public Properties()

This constructor creates an empty property list with no default values.

  • public Properties(Properties defaultValues)

This constructor creates an empty property list with the specified defaults.

2. Class Properties methods

Here are the methods provided by the class Properties, that can be used when you create instances of java.util.Properties.

  • String getProperty(String key)
    Retrieves the property with the specified key in this property list.
  • String getProperty(String key, String defaultValue)
    Retrieves the property with the specified key in this property list.
  • void list(PrintStream out)
    Prints this property list out to the specified output stream.
  • void list(PrintWriter out)
    Prints this property list out to the specified output stream.
  • void load(InputStream inStream)
    Reads a property list (key/value pairs) from the input byte stream.
  • void load(Reader reader)
    Reads a property list (key/value pairs) from the input character stream in a simple line-oriented format.
  • void loadFromXML(InputStream in)
    Loads all the properties defined in the XML document on the specified input stream into this properties table.
  • Enumeration propertyNames()
    Returns an enumeration of all the keys in this property list. This enumeration also includes all those distinct keys in the default property list that have a key with a name that has not been found in the main properties list.
  • Object setProperty(String key, String value)
    Calls the Hashtable method put, so as to set the specified value to the specified key.
  • void store(OutputStream out, String comments)
    Writes this property list (key/value pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table (when using the load(InputStream in) method).
  • void store(Writer writer, String comments)
    Writes this property list (key/value pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader in) method.
  • void storeToXML(OutputStream os, String comment)
    Produces an XML document representing all the properties contained in this properties table.
  • void storeToXML(OutputStream os, String comment, String encoding)
    Produces an XML document representing all the properties contained in this properties table, with the specified encoding.
  • Set stringPropertyNames()
    Returns a set of keys which also includes all those distinct keys in the default property list that have a key with a name that has not been found in the main properties list.

For further details for each of the above methods you can have a look at class Properties JavaDoc.

3. Examples of using Properties class

Create a java class named PropertiesExample.java with the following code:

PropertiesExample.java

import java.io.*;
import java.util.*;

public class PropertiesExample {
	public static void main(String args[]) {
		try {

			Properties grades = new Properties();

			grades.setProperty("Geometry", "20");
			grades.setProperty("Algebra", "20");
			grades.setProperty("Physics", "18");
			grades.setProperty("Chemistry", "17");
			grades.setProperty("Biology", "19");

			// Save the grades properties using store() and an output stream
			FileOutputStream out = new FileOutputStream(
					"schoolGrades.properties");
			grades.store(out, null);
			out.close();

			// load the properties file using load() and an input stream
			FileInputStream in = new FileInputStream("schoolGrades.properties");
			grades.load(in);
			in.close();

			// iterate properties file to get key-value pairs
			for (String key : grades.stringPropertyNames()) {
				String value = grades.getProperty(key);
				System.out.println("The grade in " + key + " is: " + value);
			}

			// search for key-value pair not in the list
			// you must define a default value, so as to return it
			// in case the key is not found in the main list
			String str = grades.getProperty("History", "No grade");
			System.out.println("The grade in History is " + str);

		} catch (IOException e) {

			e.printStackTrace();
		}
	}
}

Let’s give a short explanation of the above code. A properties list is created with no default values. Then, using the method setProperty(), we create key/value pairs and put them in the properties list. Afterwards, we show how to write to a properties file, using output stream and how to load this properties file using input stream. Then, we iterate the properties list so as to retrieve the key/value pairs by using method getProperty(). Finally, we show a case of a key not found in the properties list grades. In that case, you must specify a default value for that key. That default value will be returned in case the key is not found to the main list.
If we run the above code, we will have the following results:

  • Output:
The grade in Physics is: 18
The grade in Geometry is: 20
The grade in Algebra is: 20
The grade in Chemistry is: 17
The grade in Biology is: 19
The grade in History is No grade

4. Download the source code

This was an example of using class Properties from java.util package. Download the source code here: JavaUtilPropertiesExample.zip

Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button