Loading Java Properties Files

      

Java Properties files are amazing resources to add information in Java. Generally these files are used to store static information in key and value pair. Things that you do not want to hard code in your Java code goes into properties files.

Although there are multiple ways of loading properties file, I will be focusing on loading the resource bundle files from class path resources. There are advantages of adding properties files in the classpath:

  1. The properties files became a part of the deployable code (e.g. JAR file) making it easy to manage.
  2. Properties files can be loaded independent of the path of source code.

Let us see the simple way of Loading a property file in Java code. There are two ways of loading properties files in Java.
1. Using ClassLoader.getResourceAsStream()
2. Using Class.getResourceAsStream()
In our example we will use both methods to load a properties file.

Following is the content of sample properties file. The properties file will be in package net.viralpatel.resources.

net/viralpatel/resources/config.properties

hello.world=Hello World

To load properties file using Classloader, use following code:

this.getClass()
		.getResourceAsStream("/some/package/config.properties");

The Class.getResourceAsStream(name) returns an Inputstream for a resource with a given name or null if no resource with the given name is found. The name of a resource is a ‘/’-seperated path name that identifies the resource. If the name with a leading “/” indicates the absolute name of the resource is the portion of the name following the ‘/’.

In Class.getResourceAsStream(name), the rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object’s class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream(java.lang.String).

So in our example to load config.properties we will have a method loadProps1().

private Properties configProp = new Properties();
...
public void loadProps1() {
		InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties");
		try {
			configProp.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
}

To load properties file using Classloader, use following code:

this.getClass()
		.getClassLoader()
		.getResourceAsStream("some/package/config.properties");

The ClassLoader.getResourceAsStream(name) returns an Inputstream for reading the specified resource or null if the resource could not be found. The name of a resource is a ‘/’-seperated path name that identifies the resource. The name no leading ‘/’ (all namea are absolute).

So in our example to load config.properties we will have a method loadProps2().

private Properties configProp = new Properties();

public void loadProps2() {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties");
		try {
			configProp.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
}

The folder structure of our example code will be:
java-load-properties

The full Java code for testing

package net.viralpatel.java;

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

public class LoadPropertiesExample {

	private Properties configProp = new Properties();

	public static void main(String[] args) {
		LoadPropertiesExample sample = new LoadPropertiesExample();
		sample.loadProps2();
		sample.sayHello();
	}

	public void loadProps1() {
		InputStream in = this.getClass().getResourceAsStream("/net/viralpatel/resources/config.properties");
		try {
			configProp.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void loadProps2() {
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("net/viralpatel/resources/config.properties");
		try {
			configProp.load(in);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void sayHello() {
		System.out.println(configProp.getProperty("hello.world"));
	}
}

Further Reading

Java ClassLoader API
Java World – Smartly Loading Properties Files


Facebook  Twitter      Stumbleupon  Delicious
  

6 Comments on “Loading Java Properties Files”

  • rakesh juyal wrote on 26 October, 2009, 23:25

    Such a long article for so simple thing.

  • JavaMan wrote on 27 October, 2009, 0:02

    Interesante aqui te dejo mis dos Blogs. Aqui encontraras cosas tambien muy interesantes:

    http://viviendoconjavaynomoririntentandolo.blogspot.com

    http://frameworksjava2008.blogspot.com

    Saludos.

  • ppow wrote on 4 November, 2009, 17:36

    Asking the class itself for resources seems weird to me. Is there any way to get that by asking the System or by other API not connected to the class you are using it in?

  • r4ds wrote on 9 November, 2009, 9:45

    Hi..
    To load the property file in Java I think, it is very easy to do it. It will come in the form of directory when you are downloading this files..
    In Java 6 updated version, every properties class have been existed, take help form there to fulfil the requirements….

  • muski wrote on 13 November, 2009, 11:27

    Simple method to load properties file.

    Properties props = new Properties();
    props.load(new FileInputStream(“config.properties”));
    String value = props.getProperty(“prop1″);
    System.out.println(value);
    System.out.println(props.entrySet());

  • test wrote on 14 December, 2009, 22:26

    Thanks man,
    awesome post.
    helped me in reading the properties file outside the jar file.

    Thanks again

Write a Comment

Gravatars are small images that can show your personality. You can get your gravatar for free today!

Copyright © 2010 ViralPatel.net. All rights reserved.