How To Read And Write Properties File In Java

How To Read And Write Properties File In Java

Let's try to understand properties files with examples - 

  • Properties files are just plain text files with .properties extension.
  • You can store data in these files only in key/value pairs.
  • These files are mainly used to store configuration details for a java application. For example, you can store database url, username and password to this file and then use this file to get database configuration at runtime.
  • The main advantage of this approach that you can change your database any time you want by just changing the value stored in .properties file. You don't have to change the code and re-deploy the application.

How to write properties files?

package com.codemeright.properties;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class WritingProperties {

    public static void main(String[] args) {

        Properties prop = new Properties();
        FileOutputStream fos = null;

        try {

            // create new file in or open existing file from the project's root folder
            fos = new FileOutputStream("config.properties");
            
            // set properties
            prop.put("database", "localhost");
            prop.put("username", "codemeright");
            prop.put("password", "cmr@123");

            // store properties to the opened file
            prop.store(fos, "Comments");

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

How to read properties files?

package com.codemeright.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ReadingProperties {
    
    public static void main(String[] args) {
        
        Properties prop = new Properties();
        FileInputStream fis = null;

        try {

            // open existing file from the project's root folder
            fis = new FileInputStream("config.properties");
            
            // load properties
            prop.load(fis);
            
            // read property
            System.out.println(prop.getProperty("database"));
            System.out.println(prop.getProperty("username"));
            System.out.println(prop.getProperty("password"));

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

How to get all properties stored in a properties file?

package com.codemeright.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PrintAllProperties {

    public static void main(String[] args) {
        
        Properties prop = new Properties();
        FileInputStream fis = null;
        String key, value;
        
        try {

            // open existing file from the project's root folder
            fis = new FileInputStream("config.properties");
            
            // load properties
            prop.load(fis);

            // read all properties
            Enumeration<?> e = prop.propertyNames();
            while (e.hasMoreElements()) {
                key = e.nextElement().toString();
                value = prop.getProperty(key);
                System.out.println("Key: " + key + ", Value: " + value);
            }

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

How to check if a properties file contains a particular key/value?

package com.codemeright.properties;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class ContainsProperties {

    public static void main(String[] args) {

        Properties prop = new Properties();
        FileInputStream fis = null;
        String key, value;

        try {

            // open existing file from the project's root folder
            fis = new FileInputStream("config.properties");

            // load properties
            prop.load(fis);

            // check if properties file contains a key
            key = "database";
            if (prop.containsKey(key)) {
                System.out.println(key + " : Key found");
            } else {
                System.out.println(key + " : Key not found");
            }

            // check if properties file contains a value
            value = "localhost";
            if (prop.containsValue(value)) {
                System.out.println(value + " : Value found");
            } else {
                System.out.println(value + " : Value not found");
            }

        } catch (IOException io) {
            io.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

So, I hope from the above examples it's clear that what properties files are and how to use them. Once again I wanna point out that the properties files are mostly used to store configuration of your application. It makes the configuration easy to change. But, you can use it for any purpose you like.

ADVERTISEMENT

About  |  Privacy Policy  |  Disclaimer  |  © Copyright 2018. All Rights Reserved.