Java Properties Class

Introduction

The Properties class in Java, part of the java.util package, is a subclass of Hashtable that is used to maintain lists of values in which the key and the value are both String. It is typically used to manage application configuration settings, where key-value pairs represent configuration properties.

Table of Contents

  1. What is the Properties Class?
  2. Common Methods
  3. Examples of Using the Properties Class
  4. Conclusion

1. What is the Properties Class?

The Properties class is a specialized Hashtable that stores key-value pairs of strings. It provides methods to load and store properties from and to streams, making it convenient for handling configuration data in Java applications. Properties can be loaded from a file, modified during runtime, and saved back to a file.

2. Common Methods

  • load(InputStream inStream): Reads a property list (key and element pairs) from the input byte stream.
  • load(Reader reader): Reads a property list (key and element pairs) from the input character stream.
  • store(OutputStream out, String comments): Writes this property list (key and element pairs) to the output stream.
  • store(Writer writer, String comments): Writes this property list (key and element pairs) to the output character stream.
  • getProperty(String key): Searches for the property with the specified key in this property list.
  • getProperty(String key, String defaultValue): Searches for the property with the specified key in this property list, returning the default value if the property is not found.
  • setProperty(String key, String value): Calls the put method of Hashtable.
  • stringPropertyNames(): Returns a set of keys in this property list where the key and its corresponding value are strings.
  • list(PrintStream out): Prints this property list out to the specified output stream.
  • list(PrintWriter out): Prints this property list out to the specified output stream.

3. Examples of Using the Properties Class

Example 1: Loading Properties from a File

This example demonstrates how to load properties from a file.

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

public class LoadPropertiesExample {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try (FileInputStream input = new FileInputStream("config.properties")) {
            properties.load(input);
            System.out.println("Database: " + properties.getProperty("database"));
            System.out.println("User: " + properties.getProperty("user"));
            System.out.println("Password: " + properties.getProperty("password"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Example 2: Storing Properties to a File

This example shows how to store properties to a file.

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

public class StorePropertiesExample {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("database", "localhost");
        properties.setProperty("user", "admin");
        properties.setProperty("password", "admin123");

        try (FileOutputStream output = new FileOutputStream("config.properties")) {
            properties.store(output, "Database Configuration");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Example 3: Using Default Properties

This example demonstrates how to use default properties.

import java.util.Properties;

public class DefaultPropertiesExample {
    public static void main(String[] args) {
        Properties defaultProps = new Properties();
        defaultProps.setProperty("database", "localhost");
        defaultProps.setProperty("user", "admin");

        Properties properties = new Properties(defaultProps);
        properties.setProperty("password", "admin123");

        System.out.println("Database: " + properties.getProperty("database"));
        System.out.println("User: " + properties.getProperty("user"));
        System.out.println("Password: " + properties.getProperty("password"));
    }
}

Output:

Database: localhost
User: admin
Password: admin123

Example 4: Listing All Properties

This example shows how to list all properties.

import java.util.Properties;

public class ListPropertiesExample {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("database", "localhost");
        properties.setProperty("user", "admin");
        properties.setProperty("password", "admin123");

        properties.list(System.out);
    }
}

Output:

-- listing properties --
database=localhost
password=admin123
user=admin

Example 5: Retrieving Property Names

This example demonstrates how to retrieve all property names.

import java.util.Properties;
import java.util.Set;

public class PropertyNamesExample {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("database", "localhost");
        properties.setProperty("user", "admin");
        properties.setProperty("password", "admin123");

        Set<String> propertyNames = properties.stringPropertyNames();
        for (String key : propertyNames) {
            System.out.println(key + ": " + properties.getProperty(key));
        }
    }
}

Output:

database: localhost
password: admin123
user: admin

4. Conclusion

The Properties class in Java provides a convenient way to handle configuration settings as key-value pairs. By using methods to load and store properties from and to streams, developers can easily manage application settings. The examples provided demonstrate common usage patterns and highlight the capabilities of the Properties class.

Comments