19.11.14

Read properties file in Java

Function to get all the properties from a given file, and return the properties object

//The properties files are all supposed be placed under this folder - "C:\\Automation\\resources\\", hence, only the filename is passed along
public Properties getPropertyValues(String propFileName){

String propFilePath = "C:\\Automation\\resources\\" + propFileName;
System.out.println("Reading properties from - " + propFilePath);

//initializing the objects
Properties properties = new Properties();
InputStream inputStream = null;

try {

inputStream = new FileInputStream(propFilePath);

// load a properties file
properties.load(inputStream);

}catch (FileNotFoundException f) {
f.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//This will return the properties object which can then be used to get all the properties from a give file.
return properties;
}

No comments:

Post a Comment