Java properties file are used to store key-value pair configuration. java.util.Properties
class is used to work with properties file in java.
Java Properties File – java.util.Properties
In java properties file can be a normal property file with key-value pairs or it can be an XML file also.
In this java properties file example, we will show you how to write a property file in both formats and then read properties from both the configuration files.
We will also show you how to load a properties file from the classpath and how to read all the keys from the properties file.
Java Properties File 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 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
package com.journaldev.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Set; public class PropertyFilesUtil { public static void main(String[] args) throws IOException { String propertyFileName = "DB.properties"; String xmlFileName = "DB.xml"; writePropertyFile(propertyFileName, xmlFileName); readPropertyFile(propertyFileName, xmlFileName); readAllKeys(propertyFileName, xmlFileName); readPropertyFileFromClasspath(propertyFileName); } /** * read property file from classpath * @param propertyFileName * @throws IOException */ private static void readPropertyFileFromClasspath(String propertyFileName) throws IOException { Properties prop = new Properties(); prop.load(PropertyFilesUtil.class.getClassLoader().getResourceAsStream(propertyFileName)); System.out.println(propertyFileName +" loaded from Classpath::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +" loaded from Classpath::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +" loaded from Classpath::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +" loaded from Classpath::XYZ = "+prop.getProperty("XYZ")); } /** * read all the keys from the given property files * @param propertyFileName * @param xmlFileName * @throws IOException */ private static void readAllKeys(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of readAllKeys"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); Set<Object> keys= prop.keySet(); for(Object obj : keys){ System.out.println(propertyFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); keys= prop.keySet(); for(Object obj : keys){ System.out.println(xmlFileName + ":: Key="+obj.toString()+"::value="+prop.getProperty(obj.toString())); } //Now free all the resources is.close(); reader.close(); System.out.println("End of readAllKeys"); } /** * This method reads property files from file system * @param propertyFileName * @param xmlFileName * @throws IOException * @throws FileNotFoundException */ private static void readPropertyFile(String propertyFileName, String xmlFileName) throws FileNotFoundException, IOException { System.out.println("Start of readPropertyFile"); Properties prop = new Properties(); FileReader reader = new FileReader(propertyFileName); prop.load(reader); System.out.println(propertyFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(propertyFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(propertyFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(propertyFileName +"::XYZ = "+prop.getProperty("XYZ")); //loading xml file now, first clear existing properties prop.clear(); InputStream is = new FileInputStream(xmlFileName); prop.loadFromXML(is); System.out.println(xmlFileName +"::db.host = "+prop.getProperty("db.host")); System.out.println(xmlFileName +"::db.user = "+prop.getProperty("db.user")); System.out.println(xmlFileName +"::db.pwd = "+prop.getProperty("db.pwd")); System.out.println(xmlFileName +"::XYZ = "+prop.getProperty("XYZ")); //Now free all the resources is.close(); reader.close(); System.out.println("End of readPropertyFile"); } /** * This method writes Property files into file system in property file * and xml format * @param fileName * @throws IOException */ private static void writePropertyFile(String propertyFileName, String xmlFileName) throws IOException { System.out.println("Start of writePropertyFile"); Properties prop = new Properties(); prop.setProperty("db.host", "localhost"); prop.setProperty("db.user", "user"); prop.setProperty("db.pwd", "password"); prop.store(new FileWriter(propertyFileName), "DB Config file"); System.out.println(propertyFileName + " written successfully"); prop.storeToXML(new FileOutputStream(xmlFileName), "DB Config XML file"); System.out.println(xmlFileName + " written successfully"); System.out.println("End of writePropertyFile"); } } |
When we run above java properties file example program, writePropertyFile
method will write property files in both the format and it will be stored in the project root directory.
Here are the property files created from writePropertyFile
method.
DB.properties
1 2 3 4 5 6 7 |
#DB Config file #Fri Nov 16 11:16:37 PST 2012 db.user=user db.host=localhost db.pwd=password |
DB.xml
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "https://java.sun.com/dtd/properties.dtd"> <properties> <comment>DB Config XML file</comment> <entry key="db.user">user</entry> <entry key="db.host">localhost</entry> <entry key="db.pwd">password</entry> </properties> |
Notice the comments in the property file, it gets generated because we passed the comment also while writing the files. If we pass comment as null, there will be no comments in the property files.
Here is the output of the above java properties file program:
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 |
<span style="color: #008000;"><strong><code> Start of writePropertyFile DB.properties written successfully DB.xml written successfully End of writePropertyFile Start of readPropertyFile DB.properties::db.host = localhost DB.properties::db.user = user DB.properties::db.pwd = password DB.properties::XYZ = null DB.xml::db.host = localhost DB.xml::db.user = user DB.xml::db.pwd = password DB.xml::XYZ = null End of readPropertyFile Start of readAllKeys DB.properties:: Key=db.user::value=user DB.properties:: Key=db.host::value=localhost DB.properties:: Key=db.pwd::value=password DB.xml:: Key=db.user::value=user DB.xml:: Key=db.host::value=localhost DB.xml:: Key=db.pwd::value=password End of readAllKeys Exception in thread "main" java.lang.NullPointerException at java.util.Properties$LineReader.readLine(Properties.java:434) at java.util.Properties.load0(Properties.java:353) at java.util.Properties.load(Properties.java:341) at com.journaldev.util.PropertyFilesUtil.readPropertyFileFromClasspath(PropertyFilesUtil.java:31) at com.journaldev.util.PropertyFilesUtil.main(PropertyFilesUtil.java:21) </code></strong></span> |
So when we just give the file name, it looks for the file in project root directory, the same place where it stores the property files. But when we try to load a property file from the classpath, it throws NullPointerException because it tries to load the file from Classpath that is src
directory of the project.
So if we copy the properties file in the classes src directory, it’s able to load them and works fine.
In that case output of the readPropertyFileFromClasspath
method is:
1 2 3 4 5 6 |
DB.properties loaded from Classpath::db.host = localhost DB.properties loaded from Classpath::db.user = user DB.properties loaded from Classpath::db.pwd = password DB.properties loaded from Classpath::XYZ = null |
Also, notice that when we use the same Properties object to load another property file, we should clear its contents using clear()
method. If we pass any key for which there is no value stored in the properties object, it returns null.
That’s all for properties file in java.