In this tutorial, How to read and write a properties file content in Java

Java properties file reader example

In this example, you will learn how to Read key and its values from a properties file and display it to console

Let’s declare the properties file

database=mysql
hostname=localhost
username=john
password=

Here is an example and step by sep

  • Create a properties object, Properties is a data structure to store keys and values in java
  • Create URL object using ClassLoader.getSystemResource method
  • Create a InputStream using url.openStream method
  • Load the properties content into java properties object using load method
  • Add try and catch block for IOExceptions and FIleNotFoundException
  • Oneway, Print the value using getProperty with the key of a properties object
  • Another way is to iterate properties object for loop
  • First, get all keys using stringPropertyNames
  • using for loop print the key and values.

public class PropertiesReader { public static void main(String[] args) { Properties properties = new Properties(); java.net.URL url = ClassLoader.getSystemResource(“database.properties”);

    try  {
        properties.load(url.openStream());
    } catch (FileNotFoundException fie) {
        fie.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(properties.getProperty("hostname"));
    Set<String> keys = properties.stringPropertyNames();
    for (String key : keys) {
        System.out.println(key + " - " + properties.getProperty(key));
    }
}

}


Output:
```markup
localhost
hostname - localhost
password - 
database - mysql
username - john

How to read a properties file line by line in java

These example read properties in java with line by line

  • created a File object with absolute path
  • Create BufferedReader using FileReader object
  • get the First line of properties file using readLine of BufferedReader
  • Loop using while loop until the end of the line reached
  • Print each line
import java.io.*;

public class PropertiesLineReader {
    public static void main(String[] args) {
        BufferedReader br = null;
        File file = new File("B:\\database.properties");

        try {
            br = new BufferedReader(new FileReader(file));
            String line = br.readLine();


            while (line != null) {

                line = br.readLine();
                System.out.println(line);

            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

How to write a key and values to a properties file in java

In this example, You can read and write a property using

  • First create a File object
  • Create a writer object using FileWriter
  • Create properties object and add new properties or update existing properties if properties file exists
  • setProperties method do update or add key and values
  • store method of properties object writes to the properties file, You have to add the comment which appends to properties file as a comment

Here is a complete example read and write a properties file

import java.io.*;
import java.util.Properties;

public class PropertieWriter {
    public static void main(String[] args) {
        FileReader reader = null;
        FileWriter writer = null;

        File file = new File("B:\\database.properties");

        try {
            reader = new FileReader(file);
            writer = new FileWriter(file);

            Properties p = new Properties();
            p.load(reader);

            p.setProperty("name","john");
            p.store(writer,"write a file");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output of an file

#write a file
#Fri Aug 27 21:52:38 IST 2021
name=john

Conclusion

You learned to read a properties file with keys and values as well as line by line and also write key and values to the properties file