Properties Class in Java

Naveen AnamaneniUpdated · 4 min read

The Properties class in Java is used to store configuration settings as key-value pairs, where both keys and values are strings. It can load settings from a text file with the .properties extension and store them back. Programs use it for things like database URLs, the application name, or a default language, so those values can change without recompiling the code. Properties extends Hashtable<Object, Object>. In this lesson, you will learn its methods and the format of a properties file.

Declaration

Declaration
public class Properties extends Hashtable<Object, Object>

The .properties File Format

app.properties
# Lines starting with # are comments
app.name=PoolString
app.version=2.1
db.url=jdbc:mysql://localhost:3306/school
db.user=navika

Each line is key=value (a colon or a space can also separate them). Spaces around = are ignored.

Methods of Properties

MethodDescription
String getProperty(String key)It returns the value for the key, or null.
String getProperty(String key, String def)It returns the value, or def if the key is missing.
Object setProperty(String key, String value)It sets a value.
void load(Reader r) / load(InputStream in)It reads key-value pairs from a properties source.
void store(Writer w, String comments)It writes all pairs in properties format.
Set<String> stringPropertyNames()It returns all keys, including those from the defaults.

Example: Loading and Reading Settings

Let's see an example that loads settings from text. A real program would read a file with Files.newBufferedReader(Path.of("app.properties")); here the text is inline so the example is self-contained.

Config.java▶ Try it
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
 
public class Config {
    public static void main(String[] args) throws IOException {
        String file = """
            # school app settings
            app.name=PoolString
            app.version=2.1
            db.user = navika
            """;
 
        Properties props = new Properties();
        props.load(new StringReader(file));
 
        System.out.println("Name: " + props.getProperty("app.name"));
        System.out.println("User: " + props.getProperty("db.user"));
        System.out.println("Theme: " + props.getProperty("ui.theme", "light"));
 
        int version = (int) Double.parseDouble(
            props.getProperty("app.version"));
        System.out.println("Major version: " + version);
    }
}
Output
Name: PoolString
User: navika
Theme: light
Major version: 2

Explanation: load reads every key=value line and skips comments. getProperty returns strings only, so numbers must be parsed. The two-argument getProperty supplies "light" because ui.theme isn't in the file.

Example: Defaults and Saving

Defaults.java▶ Try it
import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import java.util.TreeSet;
 
public class Defaults {
    public static void main(String[] args) throws IOException {
        Properties defaults = new Properties();
        defaults.setProperty("ui.theme", "light");
        defaults.setProperty("ui.lang", "en");
 
        Properties user = new Properties(defaults);
        user.setProperty("ui.theme", "dark");
 
        System.out.println("theme: " + user.getProperty("ui.theme"));
        System.out.println("lang: " + user.getProperty("ui.lang"));
        System.out.println("keys: "
            + new TreeSet<>(user.stringPropertyNames()));
 
        StringWriter out = new StringWriter();
        user.store(out, null);
        System.out.println("Saved lines: " + out.toString().lines().count());
    }
}
Output
theme: dark
lang: en
keys: [ui.lang, ui.theme]
Saved lines: 2

Explanation: new Properties(defaults) creates settings that fall back to defaults for missing keys, so ui.lang comes from the defaults while ui.theme is overridden. store writes only the user's own settings: a timestamp comment line plus ui.theme=dark, which is 2 lines.

Common Mistakes

Using put and get instead of setProperty and getProperty. put accepts non-string values, which store then rejects.

Forgetting that values are strings. "2.1" must be parsed before doing arithmetic.

Expecting store to keep comments and order from the original file. It writes its own format.

Practice

  1. Easy: Load a properties text with width and height keys and print the area.
  2. Harder: Write a program that reads app.properties from disk (creating it with defaults if it doesn't exist), changes one setting, and saves the file again.

Quick check: Properties

4 questions
1What types are Properties keys and values meant to be?
2Which method reads a .properties file?
3What does getProperty("x", "y") return when x is missing?
4Which class does Properties extend?

Key Takeaways

Was this tutorial helpful?

Lesson 293 of 482