Properties file are often used in Java applications in the aim to store configuration parameters, internationalization and various informations. A properties file have « .properties » as extension where each line is a key/value entry. The key and the value are separated by the equal symbol « = ». Properties can also be stored in a XML file (see last chapter).
In this tutorial, we will see how to read a properties file in Java. Consider the following properties file named config.properties :
1 2 3 |
database.url=localhost database.login=roufid database.password=password |
Table of contents
1- Load properties file
Since JDK 1.0
Java offers the Properties class from the java.util package to read properties file. Below an example :
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 |
public class PropertiesExample { public static void main(String[] args) { // Properties file path. String filePath = "c:/files/config.properties"; Properties prop = new Properties(); try (InputStream inputStream = new FileInputStream(filePath)) { // Loading the properties. prop.load(inputStream); // Getting properties String url = prop.getProperty("database.url"); String login = prop.getProperty("database.login"); String password = prop.getProperty("database.password"); System.out.println("URL = " + url); System.out.println("login = " + login); System.out.println("password = " + password); } catch (IOException ex) { System.out.println("Problem occurs when reading file !"); ex.printStackTrace(); } } } |
Note that we used the With a try with resources, so no need to close the inputStream.
Output :
1 2 3 |
URL = localhost login = roufid password = password |
2- Load properties from a file located in classpath
The access to a properties file located in application classpath depends of whether your are on a static method or not.
-
Static method
In a static method, you can access a file in your classpath by using the following code :
1 |
<YOUR_CLASS_NAME>.class.getClassLoader().getResourceAsStream(<YOUR_FILE_NAME>) |
Full example :
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 |
public class PropertiesExample { public static void main(String[] args) { // Properties file path. String filePath = "config.properties"; Properties prop = new Properties(); try (InputStream inputStream = PropertiesExample.class.getClassLoader().getResourceAsStream(filePath)) { // Loading the properties. prop.load(inputStream); // Getting properties String url = prop.getProperty("database.url"); String login = prop.getProperty("database.login"); String password = prop.getProperty("database.password"); System.out.println("URL = " + url); System.out.println("login = " + login); System.out.println("password = " + password); } catch (IOException ex) { System.out.println("Problem occurs when reading file !"); ex.printStackTrace(); } } } |
-
Non-static method
In a non-static method, you can access a file in the classpath by using the following code :
1 |
getClass().getClassLoader().getResourceAsStream(<YOUR_FILE_NAME>); |
3- XML properties file
Properties can also be stored in a XML file respecting the properties dtd. Below an example :
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <entry key="database.password">password</entry> <entry key="database.login">roufid</entry> <entry key="database.url">localhost</entry> </properties> |
However, reading the properties file remains the same. Replace just
1 |
prop.load(inputStream); |
1 |
prop.loadFromXML(inputStream); |