Java properties file

from Wikipedia, the free encyclopedia

A Java properties file is a text file that is used in the Java programming language as a simple configuration mechanism. A Property (German "Property") in this context is a text that is stored under a particular name. Java properties files usually have the file extension .properties .

structure

The following example shows an excerpt from a simple configuration file:

# Dies ist ein Kommentar
! Dies ist ebenfalls ein Kommentar
wenig Text
pi  = 3.14
lang:de
vieltext Dieser Text \
         läuft über zwei Zeilen.
paramText = Text mit dynamischem Parameter: {0}
unicodeText = Ein Smiley: \u263A

The sample contains two comment lines and defines six values for the names wenig, pi, lang, vieltext, paramTextand unicodeText.

As can be seen from the example, there are comment lines that begin with a hash mark# or an exclamation mark ! , and data lines in which a name and a text are defined.

Name and text can be separated from each other in three ways, whereby the separators do not belong to the key or text.

  1. with one or more spaces : In the example above, the key has wenigthe value "Text", accordingly vieltext.
  2. by an equal sign = , surrounded by no or any number of spaces: In the above example, the key has pithe value "3.14".
  3. with a colon : , surrounded by no or any number of spaces: In the above example, the key has langthe value “de”.

A backslash \ at the end of the line means that the text continues on the next line. If there are multiple backslashes at the end of the line, the text will only continue on the next line if the number of slashes is odd. If the line ends e.g. B. on \\, the text only extends to the end of the line and contains a backslash at the end; however \\\, if the line ends , the text is continued in the next line.

ISO 8859-1 is used as the character coding for the text . Unicode characters that are not included in the ISO encoding must be encoded with an escape sequence .

Use in Java programs

Such a Java properties file can be read in using the class java.util.Properties . The defined values ​​can then be queried using their methods . The following code fragment creates a properties object, reads a file named beispiel.properties, and stores the value of the property langin a variable named sprache.

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.util.Properties;

Properties properties = new Properties();
BufferedInputStream stream = new BufferedInputStream(new FileInputStream("beispiel.properties"));
properties.load(stream);
stream.close();
String sprache = properties.getProperty("lang");

With the loadmethod, the exception must be IOExceptioncaught. For the sake of clarity, the try-catch block has been omitted.

placeholder

A non-negative whole number in curly brackets {}stands for a text that is to be added at runtime of the program. It makes sense that the numbers in brackets start with 0and are consecutive . They can appear several times and in any order . Such replacements are useful for the internationalization or localization of programs where dynamically generated text components are to be output in different languages in different places or in different order.

The parameter can be replaced with the following code:

String paramText = properties.getProperty("paramText");
String dynText = "dynamisch eingesetzter Text";
System.out.println(MessageFormat.format(paramText,dynText));

Output:

Text mit dynamischem Parameter: dynamisch eingesetzter Text

If there are several replacement parameters, an array or a set with the replacement values ​​must be transferred for "dynText" .

Strictly speaking, this is not a feature of the properties mechanism, but is mentioned here because it is used widely in this context.

internationalization

Another feature of the properties mechanism in Java is the division of the content per locale . This is indicated by a suffix in the file name. A properties file messages.propertiesspecifically for German-language texts would be messages_de.propertiesnamed. You can narrow this down even further by specifying a region. A property file for Austrians would thus named as follows: messages_de_AT.properties. A variant without a suffix should always be available as a fallback. Since Java 9, ResourceBundles properties have been decoded with UTF-8 as standard, which means that illegible escape sequences can be omitted. If problems occur during decoding, it is automatically tried again with ISO 8859-1.

Individual evidence

  1. a b c d e Properties . Java API Specification
  2. JEP 226: UTF-8 Property Resource Bundles. Retrieved June 22, 2020 .