Properties

Properties File Java Example

In this example, we shall show you how to use property files in java. Java property files are an easy and effective way
to organize key value pairs in plain text files. Property files support two formats

  • Text file format – In this  format, different key, value pairs are separated by newline character
  • XML format – This is standard XML format.

Java gives support to read and write property files via java.util.Properties class. This class extends
java.util.HashTable class and therefore supports key value pairs inherently.

Example in this post is created using Eclipse Luna and Java7.

1. Properties constructors

Properties class supports 2 constructors

  • Properties()

Default constructor. This is used to create an empty properties object. This properties object will not contain any
properties on time of its creation

  • Properties(Properties defaults)

This is used to create Properties object with a default set of properties.

2. Properties common methods

Following are some useful methods from properties class

    2.1 Reading properties

    • String getProperty(String key)

    Reads property with a given key, returns value of the property. This method returns null if property is not
    found.

    • String getProperty(String key,String default)

    Reads property with a given key, returns value of the property. This method returns default parameter passed
    in if property is not found.

    • void load(InputStream reader)

    Loads properties from a java.io.InputStream instance.

    • void loadFromXML(InputStream reader)

    Loads XML document and parse it to populate properties.

    2.2 Writing properties

    • Object setProperty(String key, String value)

    Sets property as key, value pair in Properties object. This method returns previously set value of the property
    if any

    • void store(OutputStream out,String comments)

    Stores the current property object into out OutputStream. Comments will be added into property file.

    • void storeToXML(OutputStream os,String comment)

    Stores properties in XML document using passed in OutputStream. Comment will be added as a comment in the
    document.

    2.3 Iterating over properties

    • set stringPropertyNames()

    Returns a java.util.Set of Strings, These can be iterated over using java standard for each loop.

3. Example of using Properties in java

PropertiesExample.java

package com.javacodegeeks;

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

public class PropertiesExample {
-
	public static void main(String[] s) {

		//////saving properties into example.properties file/////////
		try (OutputStream out = new FileOutputStream("example.properties")) {
			Properties properties = new Properties();
			properties.setProperty("name", "javaCodeGeeks");
			properties.setProperty("article", "JavaProperties");
			properties.setProperty("version", "1.0");
			properties.setProperty("ide", "eclipse");
			properties.store(out, "This is a sample for java properties");

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

		////////////Reading properties////////////////////////////////
		try (InputStream in = new FileInputStream("example.properties")) {
			Properties prop = new Properties();
			prop.load(in);
			System.out.println("####Properties.getProperty usage####");
			System.out.println(prop.getProperty("name"));
			System.out.println();

			System.out.println("####Properties.stringPropertyNames usage####");
			for (String property : prop.stringPropertyNames()) {
				String value = prop.getProperty(property);
				System.out.println(property + "=" + value);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println();

		////////////////////////////////////////////////////

		/////////writing and reading fromxml////////////////
		try (OutputStream out = new FileOutputStream("example.xml")) {
			Properties properties = new Properties();
			properties.setProperty("name", "javaCodeGeeks");
			properties.setProperty("article", "JavaProperties");
			properties.setProperty("version", "1.0");
			properties.setProperty("ide", "eclipse");
			properties.storeToXML(out,
					"This is how we can have properties as xml");

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

		///////////Reading properties from xml/////////////////
		try (InputStream in = new FileInputStream("example.xml")) {
			Properties prop = new Properties();
			prop.loadFromXML(in);

			System.out.println("####Properties.load from xml usage####");
			for (String property : prop.stringPropertyNames()) {
				String value = prop.getProperty(property);
				System.out.println(property + "=" + value);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println();

		///////////////////////////////////////////////////////

	}
}

4. Output

####Properties.getProperty usage####
javaCodeGeeks

####Properties.stringPropertyNames usage####
version=1.0
name=javaCodeGeeks
ide=eclipse
article=JavaProperties

####Properties.load from xml usage####
version=1.0
name=javaCodeGeeks
ide=eclipse
article=JavaProperties

5. Download source code

This was an example of java.util.properties.

Download
You can download the full source code of this example here : JavaPropertiesExample.zip

Arpit Gautam

Arpit has graduated from Computer Science and Engineering Department from the Institute of Technology and Management Gurgaon. He is working in enterprise product development since a decade and worked on desktop, mobile and server side applications using java. During his studies, he participated in various coding contests and technical paper presentations. He is working as a lead Software Engineer in Workforce Management domain where he is mainly involved with projects based on Java application and C++ system programming. He is curious about writing agile code which can adapt as business changes. He likes to experiment with open source technologies and java tech stack in his spare time.
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