Home > Uncategorized > Reading property file using ResourceBundle

Reading property file using ResourceBundle

Now it’s very easy to read the .properties file using ResourceBundle utility.

Step 1 : Create a properties file, i.e. key.properties

name=Amit
email=amitpatelinfo@gmail.com

Step 2 : Create a Property reader class. i.e.  PropertyReader.java

package com.ap.util;

import java.util.ResourceBundle;
/**
* Read the properties file.
* @author Amit Patel
*
*/

public class PropertyReader {

private static final ResourceBundle keyBundle = ResourceBundle.getBundle(“key”);

public static String getValueFromKeyFile(String key){

try{

return keyBundle.getString(key);

}catch(Exception ex){

return null;

}

}

}

Step 3 : Use the PropertyReader to read the property file.

String email= PropertyReader.getValueFromKeyFile(“email”);

That’s it. .. Wow you read the property file using few line of code.. Great..!

Advertisement
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.