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 a Properties file in Python

How to read properties in Python with examples?

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

Let’s declare the properties file

database=ostgress
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 Python

  • Create URL object using ClassLoader.getSystemResource method

  • Create an InputStream using url.openStream method

  • Load the properties content into the java properties object using the load method

  • Add try and catch blocks 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.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

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:

localhost
hostname - localhost
password -
database - mysql
username - john

How to read properties file line by line in Python

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
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 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.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 read a properties file with key and values as well as line by line and also write key and values to properties file