The Java Properties class, java.util.properties, is like a Java Map of Java String key and value pairs. The Java Properties class can write the key, value pairs to a properties file on disk, and read the properties back in again. This is an often used mechanism for storing simple configuration properties for Java applications.

Create a Properties Instance
To use the Java Properties class you must first create a Properties instance. You do so via its constructor and the Java new instruction. Here is an example of creating a Java Properties instance: For more additional info Java Certification Course
Properties properties = new Properties();
Set Properties
To set properties in a Java Properties instance you use the setProperty() method. Here is an example of setting a property (key – value pair) in a Java Properties object:
properties.setProperty("email", "myname@gmail.com");
Get Properties
To get properties from a Java Properties object you use the getProperty() method, passing the key of the property to get as parameter. Here is an example of getting a property from a Java Properties instance:
String email = properties.getProperty("email");
Remove Properties
You can remove a property from a Java Properties instance using its remove() method, passing as parameter to remove() the key for the property to remove. Here is an example of removing a property from a Java Properties instance:
properties.remove("email");
public class Properties:
extends Hashtable<Object,Object>
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its “defaults”; this second property list is searched if the property key is not found in the original property list.
Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly discouraged as they allow the caller to insert entries whose keys or values are not Strings. Learn more skills from Java Online Course
The setProperty method should be used instead. If the store or save method is called on a “compromised” Properties object that contains a non-String key or value, the call will fail. Similarly, the call to the propertyNames or list method will fail if it is called on a “compromised” Properties object that contains a non-String key.
The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding
Iterate Properties
You can iterate the keys of a Java Properties instance by obtaining the key set for the Properties instance, and iterating this key set. Here is an example of obtaining the key set of a Java Properties to iterate all its keys:
Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");
properties.setProperty("key3", "value3");
Iterator keyIterator = properties.keySet().iterator();
while(keyIterator.hasNext()){
String key = (String) keyIterator.next();
String value = properties.getProperty(key);
System.out.println(key + " = " + value );
}
Store Properties to File
You can store the property key, value pairs to a properties file which can be read again later on. You store the contents of a Properties object via its store() method. Here is an example of storing the contents of a Java Properties to a properties file:
Properties properties = new Properties();
properties.setProperty("property1", "value1");
properties.setProperty("property2", "value2");
properties.setProperty("property3", "value3");
try(FileWriter output = new FileWriter("data/props.properties")){
properties.store(output, "These are properties");
} catch (IOException e) {
e.printStackTrace();
}
Property File Encoding
By default the Java Properties file encoding is ISO-8859-1 (Latin-1). However, these days it is more common to use UTF-8 as encoding. You can specify the file encoding to use as the second parameter of the Java FileWriter used when the file is stored. Here is an example of setting the Java Properties file encoding (charset) to UTF-8: For live free demo, enroll for Learn Java Online
try(FileWriter output = new FileWriter("data/props.properties", Charset.forName("UTF-8"))){
properties.store(output, "These are properties");
} catch (IOException e) {
e.printStackTrace();
}
Property File Format
A Java Properties property file consists of lines with one key=value pair on each line. Here is an example Java Properties property file:
#These are properties
#Thu Jul 04 21:29:20 CEST 2019
property2=value2
property1=value1
property3=value3
The lines starting the # are comments. Notice the first line of the properties file is actually the comment that was passed as second parameter to the store() method call in the code example in the previous section about storing properties to a property file. The lines following the key=value format contain the property key, value pairs.
Store Properties to XML File
The Java Properties class can also write the key-value pairs stored in it to an XML file via its storeToXML(). Here is an example of storing a Java Properties to an XML file:
Properties properties = new Properties();
properties.setProperty("property1", "value1");
properties.setProperty("property2", "value2");
properties.setProperty("property3", "value3");
try(FileOutputStream output = new FileOutputStream("data/props.xml")){
properties.storeToXML(output, "These are properties");
} catch (IOException e) {
e.printStackTrace();
}
Property XML File Encoding
By default the Java Properties XML property file encoding is UTF-8. Note, that this is the reverse default of non-XML property files. If you need to use another encoding for the XML file, this is possible. You can specify the file encoding to use as the third parameter to the storeToXML method. Here is an example of storing a Java Properties to XML using the ISO-8859-1 encoding:
try(FileOutputStream output = new FileOutputStream("data/props.xml")){
properties.storeToXML(output, "These are properties", Charset.forName("ISO-8859-1"));
} catch (IOException e) {
e.printStackTrace();
}
XML Property File Format
The Java Properties object stored to XML file in the example in the previous section looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>These are properties</comment>
<entry key="property2">value2</entry>
<entry key="property1">value1</entry>
<entry key="property3">value3</entry>
</properties>
To get in-depth knowledge, enroll for live free demo on Java Online Training