HOWTO · Java

Properties File in Java

This article discusses about the properties file in Java.

On this page

Java’s Properties class stores application configuration as key-value pairs, commonly in a file with the .properties extension. Use setProperty() to add string keys and values, store() to save them, load() to read them, and getProperty() to retrieve one setting. The list() method can display the loaded entries for inspection.
Working with .properties files is covered in the file IO and properties group of the Java Tutorial, where Java configuration guides are collected.

Create and Set Values in Java Properties File

A properties file has one entry per line. For example, version=1.1 and projectName=PropertiesFileExample are separate entries; a line beginning with # or ! is a comment. The format accepts =, :, or whitespace as a key-value separator. Leading whitespace before a key is ignored, and values can contain spaces. Use setProperty() to add each pair, then store() to write the collection and an optional comment.

Run this example:

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class JavaExample {
  public static void main(String[] args) throws IOException {
    Path directory = Files.createTempDirectory("properties-demo-");
    Path file = directory.resolve("myConfigs.properties");

    Properties properties = new Properties();
    properties.setProperty("version", "1.1");
    properties.setProperty("projectName", "PropertiesFileExample");
    properties.setProperty("date", "2021/12/27");

    try (OutputStream output = Files.newOutputStream(file)) {
      properties.store(output, "Application settings");
    }
  }
}

Files.newOutputStream() creates the file when needed and truncates it when it already exists. Its parent directory must exist; this example creates a temporary directory first. The try-with-resources block closes the stream even if writing fails. If you use a fixed path instead, create its parent directory before opening the file. store() writes a comment and a generated timestamp as well as the entries, so those comment lines vary between runs. With an OutputStream, non-Latin characters are escaped using the properties-file format; a Writer lets you choose a character encoding explicitly.

If your code uses java.io, new FileOutputStream(path) has the same create-or-truncate behavior and also requires the parent directory to exist. Close it with try-with-resources just like the stream above.

Get a Property From the Java Properties File

To retrieve a setting, open the saved file for input, call load() on a Properties object, and pass the desired key to getProperty(). This example writes and reads the file in one run so it does not depend on a file left by another program. The input stream also uses try-with-resources, which releases the file after loading.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class JavaExample {
  public static void main(String[] args) throws IOException {
    Path directory = Files.createTempDirectory("properties-demo-");
    Path file = directory.resolve("myConfigs.properties");
    Properties saved = new Properties();
    saved.setProperty("date", "2021/12/27");

    try (OutputStream output = Files.newOutputStream(file)) {
      saved.store(output, "Application settings");
    }

    Properties loaded = new Properties();
    try (InputStream input = Files.newInputStream(file)) {
      loaded.load(input);
    }

    String date = loaded.getProperty("date");
    System.out.println(date == null ? "No date setting" : date);
  }
}

If the key is absent, getProperty() returns null; it does not throw an error. Check for null when the setting is optional, or supply a fallback with getProperty("theme", "light"). A missing file, unreadable path, or other file access problem is different: opening or loading it can raise an IOException. Handle that at the application boundary with a useful message or let the caller decide how to recover. The example declares IOException on main to keep the retrieval flow visible; a production application usually catches it where it can provide context.

Get All the Keys and Values From the Java Properties File

For a quick console view, load the file and call list() with System.out. The following complete example creates its own input file first, then displays the loaded entries. It includes a second setting so the output illustrates more than one key-value pair.

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class JavaExample {
  public static void main(String[] args) throws IOException {
    Path directory = Files.createTempDirectory("properties-demo-");
    Path file = directory.resolve("myConfigs.properties");
    Properties saved = new Properties();
    saved.setProperty("version", "1.1");
    saved.setProperty("projectName", "PropertiesFileExample");

    try (OutputStream output = Files.newOutputStream(file)) {
      saved.store(output, "Application settings");
    }

    Properties loaded = new Properties();
    try (InputStream input = Files.newInputStream(file)) {
      loaded.load(input);
    }
    loaded.list(System.out);
  }
}

list() is intended for a readable diagnostic listing, not as a general iteration API. Its presentation and entry order are not a stable format to parse or compare. If application code needs to process entries, iterate over the Properties entries or keys instead, and handle each value as a string. Keep the original file as the source of truth when ordering or comments matter: saving properties is for configuration data, while list() is only a view of the values currently loaded in memory.