java.util.Properties is a subclass of Hashtable<Object, Object>.This is used to maintain lists of values in which both keys and values are strings.Ideal for configuration data like settings, messages, or environment variables.Often used to read/write .properties files in Java apps.
Commonly used Constructors and Methods


Simple Program
import java.util.Properties;
public class SimplePropertiesExample {
public static void main(String[] args) {
Properties config = new Properties();
config.setProperty("username", "admin");
config.setProperty("password", "1234");
config.setProperty("timeout", "30");
System.out.println("Username: " + config.getProperty("username"));
System.out.println("Timeout: " + config.getProperty("timeout"));
}
}Output
Username: admin
Timeout: 30Code language: HTTP (http)
Problem Statement:
LotusJavaPrince and Mahesh are building a banking admin panel that uses an external .properties file for configuration settings like database URL, username, password, and application mode (dev/prod). They want a Java program that loads and displays these settings from the properties file.
config.properties File:
db.url=jdbc:mysql://localhost:3306/bank
db.user=lotusjavaprince
db.password=securepass
app.mode=productionCode language: JavaScript (javascript)
import java.util.Properties;
import java.io.FileReader;
import java.io.IOException;
public class ConfigLoader {
public static void main(String[] args) {
Properties config = new Properties();
try {
// Load from file
FileReader reader = new FileReader("config.properties");
config.load(reader);
System.out.println("Database URL: " + config.getProperty("db.url"));
System.out.println("User: " + config.getProperty("db.user"));
System.out.println("App Mode: " + config.getProperty("app.mode"));
} catch (IOException e) {
System.out.println("Error loading config file: " + e.getMessage());
}
}
}Output
Database URL: jdbc:mysql://localhost:3306/bank
User: lotusjavaprince
App Mode: productionCode language: JavaScript (javascript)
The java.util.Properties class is a powerful and lightweight tool for managing configuration data in Java applications. Whether used in-memory or with external files, it offers a simple key-value based solution that’s ideal for settings, localization, and environment configuration.
