Dynamically generating a properties file and Reading a properties file in Java using Properties java class
To generate a properties file setting the key-value attributes:
The code is self explanatory
Code snippet example #1:
Below is the code which will generate the properties file named, dbdetail.properties.
import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class GenerateProperties { public static void main( String[] args ) { Properties prop = new Properties(); try { //set the properties value prop.setProperty("databasename", "dbname"); prop.setProperty("dbuserid", "userId"); prop.setProperty("dbpassword", "passwrd"); //save properties to project root folder prop.store(new FileOutputStream("dbdetail.properties"), null); } catch (IOException ex) { ex.printStackTrace(); } } }
Output:
a file dbdetail.properties will be created with below content
#Thu Jul 25 10:09:07 PDT 2013
dbuserid=userId
databasename=dbname
dbpassword=passwrd
To read the properties file using Properties object of java
Code snippet example #2:
import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ReadProperties { public static void main( String[] args ) { Properties prop = new Properties(); try { //load a properties file prop.load(new FileInputStream("dbdetails.properties")); //get the property value and print it out System.out.println(prop.getProperty("databasename")); System.out.println(prop.getProperty("dbuserid")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } } }
Note: be sure that the location of the properties file is within the classpath.
Output will be :
dbname
userId
passwrd
There are other scenarios to load the properties file:
Instead of specifying the location of the properties file, if we include the properties file in class path we can directly read the file.
refer to the (Code snippet example #2), we are using FileInputStream instead we can use getClassLoader() to locate the properties file if its included in the classpath.
Below is the code to load the properties file when the properties file is within the class path
1. to access the properties file - static way
2. to access the propertied file within non-static way
Below is representation of 1. accessing within a static block
Code snippet example #3
import java.io.FileInputStream; import java.io.IOException; import java.utilutil.Properties; public class ReadPropFrmClasspath { public static void main( String[] args ) { Properties prop = new Properties(); try { //load a properties file from class path, inside static method prop.load(ReadPropFrmClasspath.class.getClassLoader().getResourceAsStream("dbdetail.properties");)); //get the property value and print it out System.out.println(prop.getProperty("databasename")); System.out.println(prop.getProperty("dbuserid")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } } }
Note:
prop.load(ReadPropFrmClasspath.class.getClassLoader().getResourceAsStream("dbdetail.properties");));
Output:
dbname
userId
passwrd
Below is representation of 2. accessing within a non-static block:
import java.io.FileInputStream; import java.io.IOException; import java.utilutil.Properties; public class ReadPoprFrmClassPath2 { public static void main( String[] args ) { Properties prop = new Properties(); try { //load a properties file from class path, non-static method prop.load(getClass().getClassLoader().getResourceAsStream("dbdetail.properties");)); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } } }
Note:
prop.load(getClass().getClassLoader().getResourceAsStream("dbdetail.properties");));
Output:
dbname
userId
passwrd
To read the properties file we can also use the ResourceBundle objects of java.
It all depends on the situation in the development to use.