Introduction
A .properties file is a list of keys and their values, written as text.
The format is the one read and written by the
Java
class java.util.Properties. Java programs use it for settings, and for
the translated text of their user interfaces through
PropertyResourceBundle. Build tools use it too, such as
Gradle’s
gradle.properties and wrapper file.
This page follows the documentation of Java SE 25.
Software
Any text editor can change the file. Java programs read it with
Properties.load or PropertyResourceBundle, and write it with
Properties.store.
Structure
# A comment
! Another comment
Truth = Beauty
greeting:Hello, world
fruits apple, banana, pear, \
cantaloupe, watermelon, \
kiwi, mango
cheeses
\:\= = a key made of a colon and an equals sign
Lines
A line ends with a line feed (\n), a carriage return
(\r), a carriage return followed by a line feed, or the end of the file.
White space means the space, tab and form feed characters. A line holding only white
space is ignored.
A line whose first character that is not white space is # or
! is a comment. A comment cannot be continued onto the next line.
Any other line is continued when it ends with a backslash, that is, with an odd number of backslashes before the line end. That backslash, the line end and the white space at the start of the next line are removed, and nothing is put in their place. An even number of backslashes does not continue the line: each pair stands for one backslash.
Keys and values
The key starts at the first character that is not white space and ends before the
first =, : or white space character that has no backslash
before it. White space after the key is skipped. If the next character is
= or :, it is skipped too, with the white space after it.
The rest of the line is the value. It may be empty, as for cheeses
above, whose value is the empty string.
So Truth = Beauty, Truth:Beauty and
Truth :Beauty all give the key Truth the value
Beauty. To put =, : or a space in a key, write
a backslash before it.
Escapes
Escapes are replaced after the key and value have been separated. They are like those of Java string literals:
| Written | Meaning |
|---|---|
\t | Tab |
\n | Line feed |
\r | Carriage return |
\f | Form feed |
\\ | Backslash |
\uxxxx | The UTF-16 code unit with the hexadecimal number xxxx; only one u is allowed |
The differences from Java literals are these. Octal escapes are not recognised.
\b is not a backspace: a backslash before a character that does not form
a valid escape is dropped without an error, so \b is the letter
b and \z is z. Quotation marks need no escape,
though an escaped one still gives the quotation mark. A badly formed
\u escape is an error (IllegalArgumentException).
Character encoding
The encoding depends on how the file is read:
| Read with | Encoding |
|---|---|
Properties.load(InputStream) | ISO 8859-1; other characters are written as \u escapes |
Properties.load(Reader) | Whatever the program’s reader decodes |
new PropertyResourceBundle(InputStream) | UTF-8; by default, if the bytes cannot be decoded as UTF-8, reading goes back and continues in ISO 8859-1 |
The Java system property java.util.PropertyResourceBundle.encoding,
set to UTF-8 or ISO-8859-1, makes
PropertyResourceBundle read only in that encoding and report an error
for a byte sequence that is not valid in it. Other values are ignored.
Files written by Java
Properties.store writes, in order: the program’s comment, if it gave
one, as lines starting with #; a comment line with the current date, or
with the text of the system property java.properties.date if that is
set; and one key=value line for each entry, sorted by key. Spaces in
keys and spaces at the start of values get a backslash, and so do #,
!, = and :. When writing to a byte stream, the
characters of keys and values below or above
~ are written as \u escapes, as are comment characters
outside ISO 8859-1.
The XML form
Properties.storeToXML and loadFromXML keep the same data
as XML. The document must have this document type declaration; the address in it is
not fetched:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Optional comment</comment>
<entry key="Truth">Beauty</entry>
</properties>
The document type definition it must satisfy is:
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
The default encoding is UTF-8, and a reader must accept UTF-8 and UTF-16. A
document that does not follow the definition gives
InvalidPropertiesFormatException.
Not described
Known, not yet covered
- Reference: only the Java class library’s reading and writing is described. How other libraries and frameworks that use
.propertiesfiles differ from it, or what they add to values (such as placeholders), is not covered. - Viewer: there is no viewer for properties files. A
.propertiesfile opens as plain text, without separating keys and values or replacing escapes.
Links
Software
- OpenJDK – the open-source Java platform, whose class library reads and writes the format (Windows, macOS, Linux)
Reference
- java.util.Properties – Oracle’s documentation of the line format, escapes and XML form
- java.util.PropertyResourceBundle – Oracle’s documentation of resource bundles read from properties files