Java Read and Write Properties File Example

In this Java tutorial, learn to read properties file using Properties.load() method. Also we will use Properties.setProperty() method to write a new property into the .properties file.

1. Setup

Given below is a property file that we will use in our example.


2. Reading Properties File

In most applications, the properties file is loaded during the application startup and is cached for future references. Whenever we need to get a property value by its key, we will refer to the properties cache and get the value from it.

The properties cache is usually a singleton static instance so that application does not require to read the property file multiple times; because the IO cost of reading the file again is very high for any time-critical application.

Example 1: Reading a .properties file in Java

In the given example, we are reading the properties from a file which is in the classpath. The class PropertiesCache acts as a cache for loaded properties.

The file loads the properties lazily, but only once.

import java.util.Properties;
import java.util.Set;

public class PropertiesCache
   private final Properties configProp = new Properties();
   private PropertiesCache()
      //Private constructor to restrict new instances
      InputStream in = this.getClass().getClassLoader().getResourceAsStream("");
      System.out.println("Reading all properties from the file");
      try {
      } catch (IOException e) {

   //Bill Pugh Solution for singleton pattern
   private static class LazyHolder
      private static final PropertiesCache INSTANCE = new PropertiesCache();

   public static PropertiesCache getInstance()
      return LazyHolder.INSTANCE;
   public String getProperty(String key){
      return configProp.getProperty(key);
   public Set<String> getAllPropertyNames(){
      return configProp.stringPropertyNames();
   public boolean containsKey(String key){
      return configProp.containsKey(key);

In the above code, we have used Bill Pugh technique for creating a singleton instance.

Let’s test the above code.

public static void main(String[] args)
  //Get individual properties
  //All property names

Program Output:

Read all properties from file
[lastName, technology, firstName, blog]

3. Writing into the Property File

Personally, I do not find any good reason for modifying a property file from the application code. Only time, it may make sense if you are preparing data for exporting to third party vendor/ or application that needs data in this format only.

Example 2: Java program to write a new key-value pair in properties file

So, if you are facing similar situation then create two more methods in like this:

public void setProperty(String key, String value){
  configProp.setProperty(key, value);

public void flush() throws FileNotFoundException, IOException {
	try (final OutputStream outputstream 
				= new FileOutputStream("");) {,"File Updated");
  • Use the setProperty(k, v) method to write new property to the properties file.
  • Use the flush() method to write the updated properties back into the file.
PropertiesCache cache = PropertiesCache.getInstance();
if(cache.containsKey("country") == false){
 cache.setProperty("country", "INDIA");

//Verify property

//Write to the file

Program Output:

Reading all properties from the file

And the updated properties file is:

#File Updated
#Fri Aug 14 16:14:33 IST 2020

That’s all for this simple and easy tutorial related to reading and writing property files using java.

Happy Learning !!

Oldest Most Voted
Inline Feedbacks
View all comments

Comments are closed for this article!

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.