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();
        }
    }
}

Leave a comment