This post introduces how to read properties file in java. Property file is mainly used in Java related technologies to store the configurable parameters of an application. Beside, the properties file can also be used for storing strings for Internationalization and localization, etc. I make sure that you will see properties file in almost java project.
Each entry is stored as a pair of strings (call the name/value), one storing the name, and the other storing the value. Entries are generally expected to be a single line of the form, one of the following
1 |
propertyName=propertyValue |
Let’s create a sample program that demonstrate how to read properties file in java.
Create java project
In this how to read properties file in java tutorial, we create java project in eclipse with the final directory structure like below:
Create properties file
We create properties file named myconfig.properties that contains content below:
1 2 3 4 5 6 7 |
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/javabycode jdbc.username = javabycode jdbc.password = mypassword hibernate.dialect = org.hibernate.dialect.MySQLDialect hibernate.show_sql = true hibernate.format_sql = true |
Noticed that the myconfig.properties file will be stored in the resources directory(resources/config.properties) of java project. You can see it on the project directory structure.
Create PropertiesUtils class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.javabycode.properties; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropertiesUtils { String result = ""; public String getPropValues() throws IOException { InputStream inputStream = null; String fileName = "myconfig.properties"; try { Properties prop = new Properties(); inputStream = getClass().getClassLoader().getResourceAsStream(fileName); if (inputStream != null) { prop.load(inputStream); } else { throw new FileNotFoundException("Property file '" + fileName + "' not found in the classpath"); } // get the property value and print it out System.out.println("jdbc.driverClassName="+prop.getProperty("jdbc.driverClassName")); System.out.println("jdbc.url="+prop.getProperty("jdbc.url")); System.out.println("jdbc.username="+prop.getProperty("jdbc.username")); System.out.println("jdbc.password="+prop.getProperty("jdbc.password")); System.out.println("hibernate.dialect="+prop.getProperty("hibernate.dialect")); System.out.println("hibernate.show_sql="+prop.getProperty("hibernate.show_sql")); System.out.println("hibernate.format_sql="+prop.getProperty("hibernate.format_sql")); } catch (FileNotFoundException ex) { System.err.println("Property file '" + fileName + "' not found in the classpath"); ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (inputStream !=null) { try{ inputStream.close(); }catch (Exception ex){ ex.printStackTrace(); } } } return result; } } |
You can also write the above code in main class. However, i like the way that package it in a separated class.
Create main class
To demonstrate how to read properties file in java , we create main class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javabycode.properties; public class PropertiesFileExample { public static void main(String args[]) { try { PropertiesUtils proUtils = new PropertiesUtils(); proUtils.getPropValues(); } catch (Exception ex) { ex.printStackTrace(); } } } |
Run main class as Java application. We will get the ouput such as screen shot.
That’s all on the how to read properties file in java tutorial.