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-

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:

Related Articles:
- Java Comparator example Sometimes we need to sort objects based on certain attributes...
- Reading user input in java Sometimes in our stand alone java program we need to...
- JfreeChart Series-3: How to create a dynamic 3D Pie Chart using JFreeChart library in Java You may refer to the earlier tutorials if you have...
- File upload to BLOB field and display attachment dialog box Sometimes a developer needs to provide the feature in their...
- Step by step guide to create Singleton design pattern with Java Singleton design pattern is a kind of design pattern which...
- How to test if a String is palindrome or not in Java? Steps to check if a given string is a Palindrome...
- creating non duplicate array from duplicate array in Java This example helps you to generate a fresh and non...
- 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...
- How to create Windows executable EXE from a Java Jar Creating Windows executable EXE file from a Jar file: //...
- 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...
You can subscribe by e-mail to receive news updates and breaking stories.
I find it very helpfull to solve my problem.