.properties

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 specific name. Java properties files usually have the file extension ". Properties".

Structure

The following example shows a section of a simple configuration file.

# This is a comment! This is also a comment some text pi = 3:14 lang: de much text This text \ runs over two lines. PARAMTEXT = text with dynamic parameters: { 0} The example contains two comment lines and defines five values ​​for the name little pi, long, lot of copy and PARAMTEXT.

As can be seen from the example, there are comment lines that begin with a number sign " #" or an exclamation mark "!", And data rows where a name and a text can be defined.

Name and body can be separated in three different ways, whereby the separator does not include the key, or text.

A backslash "\ " at the end of the line indicates that the text continues on the next line.

Use in Java programs

Such a Java properties file can be read by the class " java.util.Properties ". Then the values ​​defined over whose methods can be queried. The following code fragment creates a Properties object, reads a file called " beispiel.properties ", and sets the value of the property "long " in a variable named " language " from.

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 language = properties.getProperty ( "long "); In the load method, the IOException exception must be caught. For clarity, the try-catch block was omitted.

Internationalization

A non-negative integer in curly brackets "{ } " represents a text that will be added at runtime of the program. It makes sense to start the numbers in the brackets with "0" and are continuous. They may occur more than once and in any order. Such replacements are useful for internationalization and localization of programs where dynamically generating text components are to be issued in different languages ​​at different locations or in different order.

The replacement of the parameter can be realized with the following code:

String PARAMTEXT = properties.getProperty ( " PARAMTEXT "); DynText String = " dynamically inserted text"; System.out.println ( MessageFormat.format ( PARAMTEXT, dynText ) ); issue:

Text with dynamic parameters: dynamically inserted text For multiple substitution parameters is an array or a lot with the replacement values ​​must be passed for " dynText ".

Strictly speaking, this is not a feature of the Properties mechanism, but above this, as it frequently is used in this context.

432214
de