ZetCode

Java Properties

last modified July 18, 2026

In this article we show how to work with configuration properties in Java using java.util.Properties.

The Properties class is a specialized hash map for storing string key-value pairs used in application configuration. It extends java.util.Hashtable and is designed to work with .properties files and XML files. Each key and its corresponding value in a property list is a string.

A .properties file is a simple text file where configuration data is stored as key-value pairs. The file format supports comments (lines starting with # or !), multi-line values (using a trailing backslash), and natural key-value syntax (key=value or key:value).

Common use cases for the Properties class include:

Creating a properties file

Properties are typically stored in a .properties file with a simple key-value format.

config.properties
# Application configuration
app.name=My Application
app.version=2.5.0
app.author=Jan Bodnar

# Database settings
db.host=localhost
db.port=5432
db.name=zetcode
db.user=admin
db.password=secret123

# Feature flags
feature.reporting=true
feature.export=false
feature.caching=true

The properties file contains configuration settings for an application. The # character denotes comments. Key-value pairs are separated by the = sign.

Loading properties from file

The load method reads a properties file from an input stream.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = new FileInputStream("config.properties")) {
        props.load(input);
    }

    var name = props.getProperty("app.name");
    var version = props.getProperty("app.version");
    var author = props.getProperty("app.author");

    System.out.printf("Application: %s%n", name);
    System.out.printf("Version: %s%n", version);
    System.out.printf("Author: %s%n", author);
}
$ java Main.java
Application: My Application
Version: 2.5.0
Author: Jan Bodnar

We load the properties from the config.properties file using load, then retrieve individual values with getProperty.

Getting and setting properties

The getProperty and setProperty methods retrieve and modify property values. The getProperty method can also specify a default value.

Main.java
void main() {

    var props = new Properties();

    props.setProperty("db.host", "localhost");
    props.setProperty("db.port", "5432");
    props.setProperty("db.name", "zetcode");

    var host = props.getProperty("db.host");
    var port = props.getProperty("db.port");
    var name = props.getProperty("db.name");

    System.out.printf("Host: %s%n", host);
    System.out.printf("Port: %s%n", port);
    System.out.printf("Name: %s%n", name);

    // getProperty with default value
    var timeout = props.getProperty("db.timeout", "30");
    System.out.printf("Timeout: %s%n", timeout);
}
$ java Main.java
Host: localhost
Port: 5432
Name: zetcode
Timeout: 30

The setProperty method sets a property value. The getProperty with two arguments returns the second argument as a default if the key does not exist.

Storing properties

The store method writes the properties to an output stream in a format suitable for loading with load.

Main.java
void main() throws IOException {

    var props = new Properties();

    props.setProperty("app.name", "ZetCode Editor");
    props.setProperty("app.version", "1.0.0");
    props.setProperty("app.theme", "dark");
    props.setProperty("app.autosave", "true");
    props.setProperty("app.font.size", "14");

    try (var output = new FileOutputStream("app.properties")) {
        props.store(output, "ZetCode Editor Configuration");
    }

    System.out.println("Properties saved to app.properties");
}
$ java Main.java
Properties saved to app.properties
$ cat app.properties
#ZetCode Editor Configuration
#Sat Jul 18 10:30:00 CEST 2026
app.font.size=14
app.name=ZetCode Editor
app.theme=dark
app.autosave=true
app.version=1.0.0

The store method writes the properties to a file. The second parameter is a comment that appears at the top of the file, followed by a timestamp comment.

Listing properties

The list method prints the properties to an output stream, useful for debugging purposes.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = new FileInputStream("config.properties")) {
        props.load(input);
    }

    props.list(System.out);
}
$ java Main.java
-- listing properties --
feature.export=false
db.user=admin
db.host=localhost
db.port=5432
db.password=secret123
app.version=2.5.0
app.name=My Application
feature.reporting=true
app.author=Jan Bodnar
db.name=zetcode
feature.caching=true

The list method writes all properties to the specified PrintStream — in this case, standard output.

Default property values

The Properties class supports a default property set. When getProperty fails to find a key in the main property set, it looks up the key in the default set.

Main.java
void main() {

    var defaults = new Properties();
    defaults.setProperty("db.host", "localhost");
    defaults.setProperty("db.port", "3306");
    defaults.setProperty("db.timeout", "30");

    var props = new Properties(defaults);

    // override some defaults
    props.setProperty("db.host", "192.168.1.100");
    props.setProperty("db.user", "root");

    var host = props.getProperty("db.host");
    var port = props.getProperty("db.port");
    var user = props.getProperty("db.user");
    var timeout = props.getProperty("db.timeout");

    System.out.printf("Host: %s%n", host);
    System.out.printf("Port: %s%n", port);
    System.out.printf("User: %s%n", user);
    System.out.printf("Timeout: %s%n", timeout);
}
$ java Main.java
Host: 192.168.1.100
Port: 3306
User: root
Timeout: 30

The default properties are passed to the Properties constructor. Properties set in the main set override the defaults; properties not found in the main set fall back to the default set.

Iterating over properties

The stringPropertyNames method returns a set of all property keys, which can be used to iterate over the properties.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = new FileInputStream("config.properties")) {
        props.load(input);
    }

    for (var key : props.stringPropertyNames()) {
        var value = props.getProperty(key);
        System.out.printf("%s = %s%n", key, value);
    }
}
$ java Main.java
feature.export = false
db.user = admin
db.host = localhost
db.port = 5432
db.password = secret123
app.version = 2.5.0
app.name = My Application
feature.reporting = true
app.author = Jan Bodnar
db.name = zetcode
feature.caching = true

The stringPropertyNames method gives us a set of keys that we use to traverse the properties. This method, unlike the legacy propertyNames, returns only string keys and is type-safe.

Properties as XML

The Properties class can load from and store to XML files using loadFromXML and storeToXML.

Main.java
void main() throws IOException {

    var props = new Properties();

    props.setProperty("db.host", "localhost");
    props.setProperty("db.port", "5432");
    props.setProperty("db.name", "testdb");
    props.setProperty("db.user", "admin");

    try (var output = new FileOutputStream("config.xml")) {
        props.storeToXML(output, "Database Configuration");
    }

    System.out.println("Properties saved to config.xml");
}
$ java Main.java
Properties saved to config.xml
$ cat config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Database Configuration</comment>
<entry key="db.user">admin</entry>
<entry key="db.host">localhost</entry>
<entry key="db.port">5432</entry>
<entry key="db.name">testdb</entry>
</properties>

The XML format provides a structured way to store configuration data and is especially useful when properties need to be processed by XML-aware tools.

The following program loads properties from the XML file created above.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = new FileInputStream("config.xml")) {
        props.loadFromXML(input);
    }

    props.list(System.out);
}
$ java Main.java
-- listing properties --
db.user=admin
db.host=localhost
db.port=5432
db.name=testdb

The loadFromXML method reads the XML document and populates the Properties object with the key-value pairs defined in the <entry> elements.

System properties

Java provides system properties that describe the runtime environment. The static System.getProperties method returns the current system properties.

Main.java
void main() {

    var props = System.getProperties();

    System.out.printf("Java version: %s%n", props.getProperty("java.version"));
    System.out.printf("Java vendor: %s%n", props.getProperty("java.vendor"));
    System.out.printf("OS name: %s%n", props.getProperty("os.name"));
    System.out.printf("OS version: %s%n", props.getProperty("os.version"));
    System.out.printf("User home: %s%n", props.getProperty("user.home"));
    System.out.printf("User dir: %s%n", props.getProperty("user.dir"));
    System.out.printf("File separator: %s%n", props.getProperty("file.separator"));
}
$ java First.java 
Java version: 25.0.3
Java vendor: Amazon.com Inc.
OS name: Linux
OS version: 6.8.0-134-generic
User home: /home/jano
User dir: /home/jano/Documents/prog/java/test
File separator: /

System properties provide information about the Java runtime and the operating system. Individual properties can also be retrieved with System.getProperty("key").

Loading properties from classpath

In larger applications, properties files are often placed on the classpath. We load them using the classloader's getResourceAsStream method.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = Main.class
            .getResourceAsStream("/config.properties")) {

        if (input == null) {
            System.err.println("Could not find config.properties");
            return;
        }

        props.load(input);
    }

    props.list(System.out);
}

The getResourceAsStream("/config.properties") loads the file from the root of the classpath. This approach is common in deployed applications where resources are packaged inside JAR files.

Properties file format details

The .properties file format has several important rules:

details.properties
# This is a comment
! This is also a comment

# Standard format
name=John Doe

# Colon separator
title: Software Engineer

# Multi-line value
description=Java developer with over 10 years \
of experience in enterprise applications \
and web services.

# Escaped characters
message=Hello,\nWelcome to our application.\nEnjoy!

# Unicode escape
company=ZetCode\u2122

# Whitespace around separator is trimmed
location = Bratislava

The stringPropertyNames method returns a set of all property keys, which can be used to iterate over the properties.

Main.java
void main() throws IOException {

    var props = new Properties();

    try (var input = new FileInputStream("details.properties")) {
        props.load(input);
    }

    for (var key : props.stringPropertyNames()) {
        var value = props.getProperty(key);
        System.out.printf("%s = %s%n", key, value);
    }
}

The output shows that escaped sequences such as \n are interpreted as newline characters, the multi-line value is joined into a single line, and the \u2122 Unicode escape is rendered as the trademark symbol. Whitespace around the = separator is trimmed from the key location and its value.

$ java Main.java
message = Hello,
Welcome to our application.
Enjoy!
description = Java developer with over 10 years of experience in enterprise applications and web services.
name = John Doe
location = Bratislava
company = ZetCode™
title = Software Engineer

The output shows that escaped sequences such as \n are interpreted as newline characters, the multi-line value is joined into a single line, and the \u2122 Unicode escape is rendered as the trademark symbol. Whitespace around the = separator is trimmed from the key location and its value.

The example demonstrates various features of the properties file format, including comments, multi-line values, escape sequences, and Unicode escaping.

Setting properties from command line

Java allows setting system properties via the -D command-line flag. Custom properties can be passed when launching the application.

Main.java
void main() {

    var mode = System.getProperty("app.mode", "production");
    var lang = System.getProperty("app.lang", "en");
    var debug = System.getProperty("app.debug", "false");

    System.out.printf("Mode: %s%n", mode);
    System.out.printf("Language: %s%n", lang);
    System.out.printf("Debug: %s%n", debug);
}

Run the program with the -D flags to set the custom properties.

$ java -Dapp.mode=development -Dapp.lang=sk -Dapp.debug=true Main.java
Mode: development
Language: sk
Debug: true

The -D flags set system properties before the application starts. This is useful for environment-specific configuration without modifying code or property files.

Source

Java Properties - language reference

In this article we have used Java's Properties class to manage configuration data.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Java tutorials.