Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

The Properties class

Reference for java.util.Properties: load and store key-value settings, supply defaults, understand escaping and encoding, and know why store output is unordered.

Properties is a Hashtable<Object,Object> specialised for string keys and string values, with built-in support for reading and writing the .properties text format. It is the classic way to keep simple settings: db.url=jdbc:h2:mem:test, retries=3.

Use getProperty(key) and setProperty(key, value) rather than the inherited get and put, so that defaults work and non-string values cannot sneak in. Keys and values are always strings, so you convert numbers and booleans yourself.

Java
Properties props = new Properties();
props.load(reader);
int retries = Integer.parseInt(props.getProperty("retries", "3"));

Try it

Java Loading and reading properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Defaults, storing and a round trip

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class Properties extends Hashtable<Object,Object>
Properties()
Properties(Properties defaults)
Member Description
String getProperty(String key) The value, searching the defaults too; null if absent.
String getProperty(String key, String def) Same, with a fallback value.
Object setProperty(String key, String value) Stores a value; returns the previous one or null.
Set<String> stringPropertyNames() All keys with string values, including defaults.
void load(Reader) / load(InputStream) Reads the format. Reader is UTF-8 or whatever you choose; InputStream is ISO-8859-1.
void store(Writer, String comments) / store(OutputStream, String) Writes the format, with an optional comment and a date line.
void list(PrintStream) Prints for debugging.

Throws: IOException from load and store, IllegalArgumentException if load finds a malformed \uXXXX escape, NullPointerException for null keys or values.

Usage notes

  • Format. key=value, key:value or key value; lines starting with # or ! are comments; a trailing backslash continues the line; \n, \t, \\ and \uXXXX are escapes. A single backslash in a Windows path is dropped, so write C:\\temp.
  • Encoding. load(InputStream) assumes ISO-8859-1, which is why older files contain \u00e9 escapes. With Reader/Writer you pick the charset, and UTF-8 is the modern choice. See Charsets.
  • Order is not defined. store writes in whatever order the hash table has, and adds a comment with the current date, so diffs are noisy. Sort the lines yourself if you need stable files.
  • Defaults are not copied. A key found only in the defaults object is returned by getProperty but is not in keySet(), size() or store() output.
  • Strings only. put("n", 5) compiles but breaks getProperty and store. Use setProperty.
  • Thread safety. The class is synchronized (it is a Hashtable); for new concurrent code, a ConcurrentHashMap plus your own parsing is usually better.
  • Not typed or nested. For structured configuration use JSON or YAML. Properties remain popular for simple settings, resource bundles and system properties (System.getProperties(), see System.getProperty()).
  • Variable substitution (${home}) is not part of Properties; frameworks add it.