Reading values from properties file using JAVA code
In this post lets write code to read values from properties file
Example my properties file (mypropertyFile.properties) contain below data
Java output:
Code to read this properties file
package com.ghr;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadProp {
public static void main(String[] args) {
Properties prop = getPropertiesFile("C:\\Users\\ravikumar\\Desktop\\mypropertyFile.properties");
String name = prop.getProperty("name");
System.out.println("Name: "+name);
String email = prop.getProperty("email");
System.out.println("Email: "+email);
}
/**
* Reading properties file and storing in prop object
*
* @param propertiesFilePath
* @return
*/
public static Properties getPropertiesFile(String propertiesFilePath) {
Properties prop = new Properties();
InputStream in;
try {
in = new FileInputStream(propertiesFilePath);
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
}
Example my properties file (mypropertyFile.properties) contain below data
Java output:
Code to read this properties file
package com.ghr;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ReadProp {
public static void main(String[] args) {
Properties prop = getPropertiesFile("C:\\Users\\ravikumar\\Desktop\\mypropertyFile.properties");
String name = prop.getProperty("name");
System.out.println("Name: "+name);
String email = prop.getProperty("email");
System.out.println("Email: "+email);
}
/**
* Reading properties file and storing in prop object
*
* @param propertiesFilePath
* @return
*/
public static Properties getPropertiesFile(String propertiesFilePath) {
Properties prop = new Properties();
InputStream in;
try {
in = new FileInputStream(propertiesFilePath);
prop.load(in);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return prop;
}
}
Comments
Post a Comment