Properties class in Java with example

In this post, we will see about Properties class in Java with example. The Properties class in Java is commonly used to manage configuration settings in Java applications. It is a subclass of the Hashtable class that represents a persistent set of properties.

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

public class ConfigManager {

    private Properties properties;

    public ConfigManager() {
        properties = new Properties();
    }

    public void loadProperties(String filename) throws IOException {
        FileInputStream inputStream = new FileInputStream(filename);
        properties.load(inputStream);
    }

    public void saveProperties(String filename, String comments) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(filename);
        properties.store(outputStream, comments);
    }

    public String getProperty(String key) {
        return properties.getProperty(key);
    }

    public void setProperty(String key, String value) {
        properties.setProperty(key, value);
    }

}

Example of real-time use of Properties class.

The Properties class in Java is commonly used to manage configuration settings in Java applications. Here are five possible use cases for the Properties class.

1. Reading and writing application configuration settings:

Properties props = new Properties();
props.load(new FileInputStream("config.properties"));

String dbUrl = props.getProperty("db.url");
String dbUsername = props.getProperty("db.username");

props.setProperty("db.password", "newPassword");
props.store(new FileOutputStream("config.properties"), "Updated DB password");

In this example, we read application configuration settings from a file called “config.properties”. We retrieve the values of properties such as “db.url” and “db.username” using the getProperty() method. We also set a new property value for “db.password” and write the updated properties to the file using the store() method.

2. Internationalization and localization of text in applications:

Properties messages = new Properties();
messages.load(new FileInputStream("messages.properties"));

String helloMessage = messages.getProperty("hello");

Locale currentLocale = Locale.getDefault();
messages = new Properties();
messages.load(new FileInputStream("messages_" + currentLocale + ".properties"));

String goodbyeMessage = messages.getProperty("goodbye");

In this example, we load message text from a properties file called “messages.properties”. We retrieve the value of the “hello” message using the getProperty() method. We then load a localized version of the messages using a different properties file based on the user’s locale. We retrieve the value of the “goodbye” message using the getProperty() method.

3. Setting up database connection pools:

Properties poolConfig = new Properties();
poolConfig.load(new FileInputStream("db-pool.properties"));

int maxConnections = Integer.parseInt(poolConfig.getProperty("max.connections"));
String dbUrl = poolConfig.getProperty("db.url");
String dbUsername = poolConfig.getProperty("db.username");
String dbPassword = poolConfig.getProperty("db.password");

DataSource dataSource = new ComboPooledDataSource();
((ComboPooledDataSource)dataSource).setMaxPoolSize(maxConnections);
((ComboPooledDataSource)dataSource).setJdbcUrl(dbUrl);
((ComboPooledDataSource)dataSource).setUser(dbUsername);
((ComboPooledDataSource)dataSource).setPassword(dbPassword);

In this example, we load configuration settings for a database connection pool from a properties file called “db-pool.properties”. We retrieve the values of properties such as “max.connections”, “db.url”, “db.username”, and “db.password” using the getProperty() method. We then use these properties to configure a DataSource object.

4. Specifying user-defined properties for custom components:

public class MyCustomComponent extends JComponent {

    private Properties properties = new Properties();

    public MyCustomComponent(Properties properties) {
        this.properties = properties;
    }

    // ... rest of component implementation

    public String getCustomProperty(String propertyName) {
        return properties.getProperty(propertyName);
    }

}

In this example, we define a custom Swing component called MyCustomComponent. We pass in a Properties object as a constructor parameter. We can use the getProperty() method to retrieve user-defined properties from the Properties object in the getCustomProperty() method.

Configuring application logging:

Properties logConfig = new Properties();
logConfig.load(new FileInputStream("log.properties"));

String logLevel = logConfig.getProperty("log.level");
String logFile = logConfig.getProperty("log.file");

Logger logger = Logger.getLogger("myLogger");
logger.setLevel(Level.parse(logLevel));

FileHandler handler = new FileHandler(logFile);
logger.addHandler(handler);

Constructors of Properties class

The Properties class in Java has two constructors.

1. Properties()

This constructor creates an empty Properties object with no default values.

Properties props = new Properties(); // creates an empty Properties object

2. Properties(Properties defaults)

This constructor creates a Properties object with default values specified by another Properties object.

Properties defaultProps = new Properties();
defaultProps.setProperty("key1", "value1");
defaultProps.setProperty("key2", "value2");

Properties props = new Properties(defaultProps); // creates a Properties object with default properties

In this example, we create a Properties object called defaultProps with two default properties: “key1” and “key2”. We then create a new Properties object called props using the defaultProps object as the default values. When we retrieve a property value from props, it will first look in defaultProps and return the default value if the property is not found in props.

That’s all about Properties class in Java with example.

You may like.

Properties class docs.