Step 1 : Create file and put values like below in it and place it in some package.
Ex:config.properties
firstname=raj
lastname=beemi
Step 2 : Use System.getProperty("user.dir") to get the relative path of workspace . This way you can make config.properties as machine independent
Step 3 : Use below code . It has all detail about further steps
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class propfilelearning {
public static void main(String[] args) throws IOException{
try{
//Create Object of Properties Class
Properties prop = new Properties();
//Create FileInputStream object of Config/data file
pro.load(new FileInputStream("config.properties"));
// Use getProperty method of Properties object to get the values.
System.out.println(prop.getProperty("firstname"));
System.out.println(prop.getProperty("lastname"));
} catch (Exception e){
e.printStackTrace();
}
}
No comments:
Post a Comment