Skip to main content Skip to docs navigation
TechSpiderTutorials

java.util.Properties Class

java.util.Properties Class

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Creating Objects

Properties prop = new Properties();

java.util.Properties Class Methods

public String getProperty(String key)

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.


public Enumeration<?> propertyNames()

Returns an enumeration of all the keys in this property list, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list.


public Set<Strin> stringPropertyNames()

Returns a set of keys in this property list where the key and its corresponding value are strings, including distinct keys in the default property list if a key of the same name has not already been found from the main properties list. Properties whose key or value is not of type String are omitted.


public Object setProperty(String key,String value)

Calls the Hashtable method put. Provided for parallelism with the getProperty method. Enforces use of strings for property keys and values. The value returned is the result of the Hashtable call to put.

Properties Class Example

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

public class PropertiesExample {

    public static void main(String[] args) {
        // Create a Properties object
        Properties properties = new Properties();

        // Add properties
        properties.setProperty("database.url", "jdbc:mysql://localhost:3306/mydatabase");
        properties.setProperty("database.username", "admin");
        properties.setProperty("database.password", "password123");

        // Store properties to a file
        try (OutputStream output = new FileOutputStream("config.properties")) {
            properties.store(output, "Database Configuration");
            System.out.println("Properties saved successfully to config.properties file.");
        } catch (IOException io) {
            System.err.println("Error storing properties: " + io.getMessage());
        }

        // Load properties from file
        Properties loadedProperties = new Properties();
        try (InputStream input = new FileInputStream("config.properties")) {
            loadedProperties.load(input);
            System.out.println("\nLoaded Properties:");
            loadedProperties.forEach((key, value) -> System.out.println(key + ": " + value));
        } catch (IOException io) {
            System.err.println("Error loading properties: " + io.getMessage());
        }

        // Access specific property
        String dbUrl = loadedProperties.getProperty("database.url");
        String dbUsername = loadedProperties.getProperty("database.username");
        String dbPassword = loadedProperties.getProperty("database.password");

        System.out.println("\nDatabase Configuration:");
        System.out.println("URL: " + dbUrl);
        System.out.println("Username: " + dbUsername);
        System.out.println("Password: " + dbPassword);
    }
}

Output:

Properties saved successfully to config.properties file.
Loaded Properties:
database.username: admin
database.password: password123
database.url: jdbc:mysql://localhost:3306/mydatabase

Database Configuration:
URL: jdbc:mysql://localhost:3306/mydatabase
Username: admin
Password: password123