How to read Properties file from resource folder in Java?
UPDATED: 19 October 2015
Tags:
FileInputStream
,
io
What is Properties file?
Properties file used for configurable parameters of your application/project like database host, username and password.
Scenario
For security reason you've to change your database password weekly or monthly. Consider if you assign password in variable[hard coded] of your program then you've to shut down the whole system and start all over again, Its not feasible when you are dealing with production environment(live server), you can't shut down live server. In that case .properties files help you to deal with this kinda situation. When you change value in .properties file you don't have to restart the system. Program will read the updated value.
common.properties
Create resource folder in your project and add common.properties file in it. [How to create resource folder in Netbeans?]
Source code up-to JDK 1.6 (ReadPropertiesFile.java)
Source code JDK 1.7 or above (ReadPropertiesFile.java)
Using try-with-resource in Java 1.7 or above
Output
How to create resource folder in Netbeans?
Properties file used for configurable parameters of your application/project like database host, username and password.
Scenario
For security reason you've to change your database password weekly or monthly. Consider if you assign password in variable[hard coded] of your program then you've to shut down the whole system and start all over again, Its not feasible when you are dealing with production environment(live server), you can't shut down live server. In that case .properties files help you to deal with this kinda situation. When you change value in .properties file you don't have to restart the system. Program will read the updated value.
common.properties
Create resource folder in your project and add common.properties file in it. [How to create resource folder in Netbeans?]
#Database credentials host=localhost username=root password=root
Source code up-to JDK 1.6 (ReadPropertiesFile.java)
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ReadPropertiesFile { /* Create basic object */ ClassLoader objClassLoader = null; Properties commonProperties = new Properties(); public ReadPropertiesFile() { /* Initialize 'objClassLoader' once so same object used for multiple files. */ objClassLoader = getClass().getClassLoader(); } public String readKey(String propertiesFilename, String key){ /* Simple validation */ if (propertiesFilename != null && !propertiesFilename.trim().isEmpty() && key != null && !key.trim().isEmpty()) { /* Create an object of FileInputStream */ FileInputStream objFileInputStream = null; /** * Following try-catch is used to support upto 1.6. * Use try-with-resource in JDK 1.7 or above */ try { /* Read file from resources folder */ objFileInputStream = new FileInputStream(objClassLoader.getResource(propertiesFilename).getFile()); /* Load file into commonProperties */ commonProperties.load(objFileInputStream); /* Get the value of key */ return String.valueOf(commonProperties.get(key)); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); }finally{ /* Close the resource */ if(objFileInputStream != null){ try { objFileInputStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } return null; } public static void main(String[] args) { /* Create object of ReadResourceFile */ ReadPropertiesFile objPropertiesFile = new ReadPropertiesFile(); /* Will give you 'null' in case key not available */ System.out.println("Database Driver: "+ objPropertiesFile.readKey("common.properties", "dbdriver")); /* Read values from resource folder */ System.out.println("Host: "+ objPropertiesFile.readKey("common.properties", "host")); System.out.println("Username: "+ objPropertiesFile.readKey("common.properties", "username")); try { while (true) { /* Print the password from commom.properties file */ System.out.println("Password: "+ objPropertiesFile.readKey("common.properties", "password")); /* Put the current thread in sleep for 5 seconds and change the value of 'password' */ Thread.sleep(5000); } } catch (Exception e) { e.printStackTrace(); } } }
Source code JDK 1.7 or above (ReadPropertiesFile.java)
Using try-with-resource in Java 1.7 or above
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; public class ReadPropertiesFile { /* Create basic object */ ClassLoader objClassLoader = null; Properties commonProperties = new Properties(); public ReadPropertiesFile() { /* Initialize 'objClassLoader' once so same object used for multiple files. */ objClassLoader = getClass().getClassLoader(); } public String readKey(String propertiesFilename, String key){ if (propertiesFilename != null && !propertiesFilename.trim().isEmpty() && key != null && !key.trim().isEmpty()) { /* try-with-resource in JDK 1.7 or above */ try( FileInputStream objFileInputStream = new FileInputStream(objClassLoader.getResource(propertiesFilename).getFile()); ){ /* Load file into commonProperties */ commonProperties.load(objFileInputStream); /* Get the value of key */ return String.valueOf(commonProperties.get(key)); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } return null; } public static void main(String[] args) { /* Create object of ReadResourceFile */ ReadPropertiesFile objPropertiesFile = new ReadPropertiesFile(); /* Will give you 'null' in case key not available */ System.out.println("Database Driver: "+ objPropertiesFile.readKey("common.properties", "dbdriver")); /* Read values from resource folder */ System.out.println("Host: "+ objPropertiesFile.readKey("common.properties", "host")); System.out.println("Username: "+ objPropertiesFile.readKey("common.properties", "username")); try { while (true) { /* Print the password from commom.properties file */ System.out.println("Password: "+ objPropertiesFile.readKey("common.properties", "password")); /* Put the current thread in sleep for 5 seconds and change the value of 'password' */ Thread.sleep(5000); } } catch (Exception e) { e.printStackTrace(); } } }
Output
Database Driver: null Host: localhost Username: root Password: root Password: root Password: admin123References
How to create resource folder in Netbeans?
Tags:
FileInputStream
,
io
0 comments :