In this tutorial you will see how to read a properties file in Java. If you have any configurable data in your application like DB configuration, user settings its better to keep it in a properties file and read it from there. A properties file stores data in the form of key/value pair.
For reading a properties file in Java there are two ways to load properties file-
- Loading properties file from the file system. See example.
- Loading properties file from classpath. See example.
Project structure
For this example we’ll have a properties file named app.properties file in a folder called resource. The resource folder is at the same level at the src folder in the Java project.
Steps for reading a properties file in Java
- Create an instance of Properties class.
- Create a FileInputStream by opening a connection to the properties file.
- Read property list (key and element pairs) from the input stream using load() method of the Properties class.
Content of the properties file
Here the properties file used is named app.properties file with it’s content as-
user=TestUser url=https://www.netjstech.com
Loading properties file from the file system
One way to read properties file in Java is to load it from the file system.
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropDemo { private Properties properties = new Properties(); public static void main(String[] args) { PropDemo pDemo = new PropDemo(); pDemo.loadPropertiesFile(); pDemo.readProperties(); } // This method is used to load the properties file private void loadPropertiesFile(){ InputStream iStream = null; try { // Loading properties file from the path (relative path given here) iStream = new FileInputStream("resource/app.properties"); properties.load(iStream); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(iStream != null){ iStream.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * Method to read the properties from a * loaded property file */ private void readProperties(){ System.out.println("User name - " + properties.getProperty("user")); System.out.println("URL - " + properties.getProperty("url")); // reading property which is not there System.out.println("City - " + properties.getProperty("city")); } }
Output
User name - TestUser URL - https://www.netjstech.com City - null
Here you can see that in the code there is an attempt to read the property “city” which doesn’t exist in the app.properties file that’s why it is retrieved as null.
Loading properties file from classpath
If you have properties file in the project classpath then you can load it by using the getResourceAsStream method. That is another way to read properties file in Java.
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class PropDemo { private Properties properties = new Properties(); public static void main(String[] args) { PropDemo pDemo = new PropDemo(); pDemo.loadProperties(); pDemo.readProperties(); } // This method is used to load the properties file private void loadProperties(){ InputStream iStream = null; try { // Loading properties file from the classpath iStream = this.getClass().getClassLoader() .getResourceAsStream("app.properties"); if(iStream == null){ throw new IOException("File not found"); } properties.load(iStream); } catch (IOException e) { e.printStackTrace(); }finally { try { if(iStream != null){ iStream.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * Method to read the properties from a * loaded property file */ private void readProperties(){ System.out.println("User name - " + properties.getProperty("user")); System.out.println("URL - " + properties.getProperty("url")); } }
Output
User name - TestUser URL - https://www.netjstech.com
That's all for this topic How to Read Properties File in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
>>>Return to Java Programs Page
Related Topics
You may also like-