Java

How to read properties file in Java

The “Properties” class in java is a child or subclass of the Hashtable class. In java, Hashtable is a predefined class that specifies the objects as a key-value pair. Same does the Properties class. The Properties class maintains the list of keys and values where both values, as well as, the keys are stored as strings. The Properties class provides several methods that are used to store the data in the properties files or to get the data from the properties files.

This write-up will provide a profound understanding of the below-listed concepts:

So let’s start!

Properties file in Java

These files are frequently used in java-oriented technologies. The properties file is used to save the application’s configurable parameters. It contains a key-value pair. The extension of the properties file is “.properties”.

How to create a properties file in java manually

In this write-up we will create a properties file using NetBeans; however, you can use any IDE of your choice such as eclipse, Netbeans, etc. To create a properties file we have to go through the following steps:

First Step

Firstly, you have to select the File and then click on the new File option, or you can simply press the “CTRL+N” to create a new file:

Second Step

Choose “category” as “other” and “file type” as “properties”:

Third Step

Specify the file name and click on the “Finish” button:

Fourth Step

Clicking on the finish button will create the properties file:

In the properties file, we added some properties as key-value pairs.

How to use store() method to create a properties file

In the above section, we have learned how to create a properties file in netbeans. However, we can use the java’s predefined methods to generate a properties file. Let’s consider the below-given code block for a profound understanding of this concept:

publicclassCreatePropertiesFile {
publicstaticvoidmain(String[] args) throws Exception {

        Properties prop = new Properties();
prop.setProperty("Name", "Mike");
prop.setProperty("Password", "[email protected]");
prop.setProperty("EmailAdress", "[email protected]");
prop.store(newFileWriter("data.properties"), "How to Create Properties File");

    }
}

In this example, we performed the following tasks:

  • Firstly, we created an object of the properties class.
  • Afterward, we utilized the setProperty() method to set the system properties.
  • Finally, we utilized the store() method to store the data into the “data.properties” file.
  • Consequently, the above code will generate the following properties file:

How to read data from Properties file in Java

Now, we will create a java class named “ReadProperties” and within that class, we will write the code to read the data from the properties file:

packagereadproperties;  
importjava.io.FileReader;
importjava.util.Properties;
publicclassReadProperties {

publicstaticvoidmain(String[] args)throws Exception{  
FileReaderreadFile=new FileReader("C:\\Users\\DELL\\Documents\\ReadProperties
    \\src\\readproperties\\newproperties.properties"
);  

    Properties prop=new Properties();  
prop.load(readFile);  

System.out.println(prop.getProperty("userName"));  
System.out.println(prop.getProperty("password"));  
  }
}

In the above code snippet:

  • Initially, we created an object of the FileReader class.
  • We passed the path of the properties file to that object.
  • Next, we created an object of the properties file.
  • Afterward, we utilized the load() method to read/load the data from the properties file.
  • Finally, we utilized the getProperty() method and passed it a key, consequently, we will get the value corresponding to that key.

The above snippet verifies that the getProperty() method returns the values associated with the specified keys (i.e. userName, and password).

Conclusion

In Java, the properties file stores the configurable parameters(key-value pair) of an application. In java, the “Properties” class is a child or subclass of the Hashtable class. It specifies the objects as a key-value pair. The “Properties” class provides several methods that are used to work with the properties files. This article explained various aspects of properties file such as how to create a properties file, and how to read data from the properties file.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.