Update Specific Values in Java Properties File: Step-by-Step Guide

HOME

We want to replace the password from Admin to Admin123.

 Properties properties = new Properties();

 FileInputStream fileInputStream = new FileInputStream("src/test/resources/userCreated.properties");
 properties.load(fileInputStream);

properties.setProperty("password", "Admin123");

FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreated.properties");

// store() method is used to write the properties into properties file
properties.store(fileOutputStream, "File is modified");
package org.example;

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

public class ModifyPropertiesExample {

    public static void main(String args[]) {

        // Creating properties files from Java program
        Properties properties = new Properties();

        try {
            FileInputStream fileInputStream = new FileInputStream("src/test/resources/userCreated.properties");
            properties.load(fileInputStream);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        properties.setProperty("password", "Admin123");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreated.properties");

            // store() method is used to write the properties into properties file
            properties.store(fileOutputStream, "File is modified");

            fileOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Java Properties: Storing and Writing Data in XML Format

HOME

Properties properties = new Properties();
properties.setProperty("username", "Vibha");
properties.setProperty("password", "XML");
 FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreatedXML.xml");

properties.storeToXML(fileOutputStream, "Sample XML Properties file created");
package org.example;

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

public class WritePropertiesXMLExample {

    public static void main(String args[]) {

        Properties properties = new Properties();
        properties.setProperty("username", "Vibha");
        properties.setProperty("password", "XML");

        try {
            // userCreated.properties is created at the mentioned path
            FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreatedXML.xml");

            // storeToXML() method is used to write the properties into properties xml file
            properties.storeToXML(fileOutputStream, "Sample XML Properties file created");
            fileOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

How to Create and Modify Properties File in Java

HOME

package org.example;

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

public class WritePropertiesExample {

    public static void main(String args[]) {

        // Creating properties files from Java program
        Properties properties = new Properties();

        try {
            // userCreated.properties is created at the mentioned path
            FileOutputStream fileOutputStream = new FileOutputStream("src/test/resources/userCreated.properties");

            properties.setProperty("username", "Vibha");
            properties.setProperty("password", "Admin");

            // store() method is used to write the properties into properties file
            properties.store(fileOutputStream, "Sample way of creating Properties file from Java program");

            fileOutputStream.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

How to read property file in Java

HOME

database.baseUrl = https://localhost
database.port = 8080
database.username = admin
database.password = Admin123
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadPropertiesExample {

    public static void main(String[] args) {

        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("src/test/resources/config.properties");

            // Load the properties file
            properties.load(inputStream);

            // Access properties
            String dbUrl = properties.getProperty("database.baseUrl");
            String dbPort = properties.getProperty("database.port");
            String dbUser = properties.getProperty("database.username");
            String dbPassword = properties.getProperty("database.password");

            // Print properties to verify
            System.out.println("Database URL: " + dbUrl);
            System.out.println("Database Port: " + dbPort);
            System.out.println("Database User: " + dbUser);
            System.out.println("Database Password: " + dbPassword);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}