.properties
Encyclopedia
.properties is a file extension for files
Computer file
A computer file is a block of arbitrary information, or resource for storing information, which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished...

 mainly used in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 related technologies to store the configurable parameters of an application
Application software
Application software, also known as an application or an "app", is computer software designed to help the user to perform specific tasks. Examples include enterprise software, accounting software, office suites, graphics software and media players. Many application programs deal principally with...

. They can also be used for storing strings for Internationalization and localization
Internationalization and localization
In computing, internationalization and localization are means of adapting computer software to different languages, regional differences and technical requirements of a target market...

; these are known as Property Resource Bundles.

Each parameter is stored as a pair of string
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

s, one storing the name of the parameter (called the key), and the other storing the value.

Format

Each line in a .properties file normally stores a single property. Several formats are possible for each line, including key=value, key = value, key:value, and key value.

.properties files can use the number sign
Number sign
Number sign is a name for the symbol #, which is used for a variety of purposes including, in some countries, the designation of a number...

 (#) or the exclamation mark
Exclamation mark
The exclamation mark, exclamation point, or bang, or "dembanger" is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or high volume , and often marks the end of a sentence. Example: “Watch out!” The character is encoded in Unicode at...

 (!) as the first non blank character
Character (computing)
In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written form of a natural language....

 in a line to denote that all text following it, is a comment
Comment (computer programming)
In computer programming, a comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program. Those annotations are potentially significant to programmers but typically ignorable to compilers and interpreters. Comments are usually...

. The backwards slash is used to escape a character. An example of a properties file is provided below.

# You are reading the ".properties" entry.
! The exclamation mark can also mark text as comments.
website = http://en.wikipedia.org/
language = English
# The backslash below tells the application to continue reading
# the value onto the next line.
message = Welcome to \
Wikipedia!
# Add spaces to the key
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Unicode
tab : \u0009

In the example above, website would be a key, and its corresponding value would be http://en.wikipedia.org/. While the number sign and the exclamation mark marks text as comments, it has no effect when it is part of a property. Thus, the key message has the value Welcome to Wikipedia! and not Welcome to Wikipedia. Note also that all of the whitespace in front of Wikipedia! is excluded completely.

In Apache Tomcat
Apache Tomcat
Apache Tomcat is an open source web server and servlet container developed by the Apache Software Foundation...

 the exclamation mark denotes a Negation
Negation
In logic and mathematics, negation, also called logical complement, is an operation on propositions, truth values, or semantic values more generally. Intuitively, the negation of a proposition is true when that proposition is false, and vice versa. In classical logic negation is normally identified...

 operator when used as the first non blank character
Character (computing)
In computer and machine-based telecommunications terminology, a character is a unit of information that roughly corresponds to a grapheme, grapheme-like unit, or symbol, such as in an alphabet or syllabary in the written form of a natural language....

 in a line.


//-------------------------------------------------
Properties myProps = new Properties;
FileInputStream MyInputStream = new FileInputStream("myPropertiesFile.properties");
myProps.load(MyInputStream);
String myPropValue = myProps.getProperty("propKey");
//-------------------------------------------------
String key = "";
String value = "";
for (Map.Entry propItem : myProps.entrySet)
{
key = (String) propItem.getKey;
value = (String) propItem.getValue;
}
//-------------------------------------------------
MyInputStream.close; // better in finally block
//-------------------------------------------------
myProps.setProperty("propKey", "myNewPropValue");
FileOutputStream MyOutputStream = new FileOutputStream("yourPropertiesFile.properties");
myProps.store(MyOutputStream, "myAddedKey: myAddedValue");
//-------------------------------------------------
MyOutputStream.close; // better in finally block
//-------------------------------------------------


The encoding of a .properties file is ISO-8859-1
ISO/IEC 8859-1
ISO/IEC 8859-1:1998, Information technology — 8-bit single-byte coded graphic character sets — Part 1: Latin alphabet No. 1, is part of the ISO/IEC 8859 series of ASCII-based standard character encodings, first edition published in 1987. It is informally referred to as Latin-1. It is generally...

, also known as Latin-1. All non-Latin-1 characters must be entered by using Unicode
Unicode
Unicode is a computing industry standard for the consistent encoding, representation and handling of text expressed in most of the world's writing systems...

 escape characters, e. g. \uHHHH where HHHH is a hexadecimal index of the character in the Unicode character set. This allows for using .properties files as resource bundles
Java resource bundle
A resource bundle is a Java .properties file that contains locale-specific data. It is a way of internationalising a Java application by making the code locale-independent.- Benefits of using resource bundles :...

 for localization
Internationalization and localization
In computing, internationalization and localization are means of adapting computer software to different languages, regional differences and technical requirements of a target market...

. A non-Latin-1 text file can be converted to a correct .properties file by using the native2ascii tool that is shipped with the JDK or by using a tool, such as po2prop, that manages the transformation from a bilingual localization format into .properties escaping.

An alternative to using unicode escape characters for non-Latin-1 character in ISO 8859-1 character encoded Java *.properties files is to the use the JDK's XML Properties file format which by default is UTF-8 encoded, introduced starting with Java 1.5.

Adobe Flex uses .properties files as well, but here they are UTF-8 encoded.

External links

- gives the precise semantics of well-formed Java property files - describes property resource bundles - explains Java properties in a simple XML format.
  • DOKSoft Properties Editor - useful utility to view and to edit properties files in a whole project.
  • Message Editor - it manages i18n for Java application. It generates XML property files. It includes two stylesheets for .properties and .xml property generation at compile time (Ant based.)
  • JLokalize - open source Java properties editor with reversal function and spell check
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK