In this tutorial, You will Learn

  • How to read properties file using configparser in python
  • Parse Properties file using JProperties library in phyton
  • Write to Properties file in Python

How to read properties in Python with example?

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

Let’s declare properties file


Here is an example and step by sep

  • Create a properties object, Properties is an data strucure to store key 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 key of an 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(); url = ClassLoader.getSystemResource(“”);

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


hostname - localhost
password - 
database - mysql
username - john

How to read properties file line by line in java

This example examples read properties in java with line by line

  • created a File object with absolute path
  • Create BufferedReader using FileReader object
  • get First line of properties file using readLine of BufferedReader
  • Loop using while loop until end of line reached
  • Print each line

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

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

            while (line != null) {

                line = br.readLine();

        } catch (FileNotFoundException e1) {
        catch (IOException e) {
        } finally {
            try {
            } catch (IOException e) {


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

In this example, You can read and write an properties using

  • First create an 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 properties file, You have to add the comment which append to properties file as a comment

Here is an complete example read and write an properties file

import java.util.Properties;

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

        File file = new File("B:\\");

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

            Properties p = new Properties();

  ,"write a file");
        } catch (FileNotFoundException e) {
        } catch (IOException e) {

Output of an file

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


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