Saturday, October 22, 2011

Read Properties file in Java



package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
public class ReadPropetiesFile {
public static void main(String a[]){
String fileName = "test.properties";
ReadPropetiesFile readProp = new ReadPropetiesFile();
readProp.readProperties(fileName);
}
public void readProperties(String fileName){
try {
ResourceBundle labels = ResourceBundle.getBundle(fileName, Locale.ENGLISH);
Enumeration bundleKeys = labels.getKeys();
while (bundleKeys.hasMoreElements()) {
   String key = (String)bundleKeys.nextElement();
   String value = labels.getString(key);
   System.out.println("key = " + key + ", " +   "value = " + value);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}

No comments: