Friday, June 6, 2014

Java - To Load a property file

Property files are used to store static information in a key-value pair. Things that you do not want to hard code in your Java code, which tend to change in future goes into properties files. Also as it is a static information you do not want to load the property file for each and every request as it may hamper performance. You may need it to be loaded only once at the begining of the loading of application.

You can load properties file using below methods. You can provide absolute path as well as relative path however mostly relative path is preferred.

prop.load(AppProperties.class.getClassLoader().getResourceAsStream("app.properties"));
OR
prop.load(new FileInputStream(new File("C:/temp/app.properties")));

Please see the self explanatory java code below - 

No comments:

Post a Comment