There are many ways to read properties file in
java. Here explained two way, using
1. ResourceBundle
2. Properties Class
How to use this tutorial
1. Create one directory src and put both below files (MyProp.properties and ReadPropFile.java)
2. MyProp.properties
name = Binod Kumar Suman
roll = 110
city = Bangalore
3. ReadPropFile.java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;
public class ReadPropFile {
public static void main(
String[] args) {
getData();
}
public static void getData(){
try{
Properties propertiesFile = new Properties();
propertiesFile.load(new FileInputStream("src/MyProp.properties"));
String studentName = propertiesFile.getProperty("name");
String roll = propertiesFile.getProperty("roll");
System.out.println("Student Name :: "+studentName);
System.out.println("Roll Number :: "+roll);
//Fetch all the Properties.
String key;
Enumeration e = propertiesFile.propertyNames();
while (e.hasMoreElements()) {
key = (String)e.nextElement();
System.out.println(key+" "+propertiesFile.getProperty(key));
}
}catch(IOException e){
e.printStackTrace();
}
}
}
Thanks,
Binod Suman
http://binodjava.blogspot.com/2009/05/how-to-read-properties-file-in-java.html