You are here: Home // Programming // How to load a properties file in Java

How to load a properties file in Java

This tutorial will explain how to access properties file through Java coding. A properties file can be accessed with the ResourceBundle class which resides in the java.util package. In the example below I’m going to provide is based on the properties file staying in the same package. For properties file staying outside the package, we need to set the CLASSPATH variable pointing to the same folder where the external properties file is staying.
First we should have the project structure like the one given below-

 properties1


And here are the code samples of all the classes.
First the properties file having some sample values as key-value structures.

TCTALK.properties

 

 

NAME=TECHCUBETALK

QUOTE=I Love TechCuBeTalk.com

 

PropertyReader.java-

package com.techcubetalk.app.util;

 

import java.util.MissingResourceException;

import java.util.ResourceBundle;

 

/**

 * PropertyReader.java - [This static utility class loads and

 * reads the values from the application properties file]

 *

 * @author TechCuBeTalk

 * @version 1.0

 */

public class PropertyReader {

      private static final String BUNDLE_NAME = “com.techcubetalk.app.resource.TCTALK”;

      private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

     

      public static String getValue(String key){

            try{

                  return RESOURCE_BUNDLE.getString(key);

            }catch(MissingResourceException mex){

                  return “”;

            }catch(Exception ex){

                  return “NOT FOUND”;

            }

      }

}

 

 

 

This class loads the properties file from the system. This is a static utility class. In the bundle name we’ve given the fully qualified package name with properties file name. Please note here we have mentioned the name only and not the extension “properties”. getValue() method takes a string key and returns the corresponding value from the properties file. If it can’t load the properties file or can’t find the key it throws MissingResourceException.
Now the invoker main class file-
PropertyReaderMain.java

 

package com.techcubetalk.app.main;

 

import com.techcubetalk.app.util.PropertyReader;

 

 

public class PropertyReaderMain {

 

      public static void main(String[] args) {

            System.out.println(“Testing Property Reader…”);

            System.out.println(“Invoking key …”);

            System.out.println(“Name=”+PropertyReader.getValue(“NAME”));

            System.out.println(“Quotation=”+PropertyReader.getValue(“QUOTE”));

      }

}

 

 

 This is self explanatory.
To know about ResourceBundle class go to http://java.sun.com/j2se/1.4.2/docs/api/java/util/ResourceBundle.html

Here is the output:

output

Blog Widget by LinkWithin

Related Articles:

  1. File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
  2. How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...
  3. Reading user input in java Sometimes in our stand alone java program we need to...
  4. Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern...
  5. creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...
  6. JfreeChart Series-2: How to create a dynamic Pie Chart using JFreeChart library in Java  In the earlier tutorial I showed you how to create...
  7. JfreeChart Series-1: How to create a dynamic Bar Chart using JFreeChart library in Java In Java creating any graph or chart like bar chart...
  8. How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...

Tags: , , ,

1 Response to " How to load a properties file in Java "

  1. Vineet says:

    I find it very helpfull to solve my problem.

Leave a Reply

Spam Protection by WP-SpamFree

Copyright © 2009 TechCuBeTalk free technology magazine. All rights reserved.
Designed by Theme Junkie. Powered by WordPress.