Properties Class in Java - GeeksforGeeks

24 Nov, 2020
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. It belongs to java.util package. Properties define the following instance variable. This variable holds a default property list associated with a Properties object. 
Properties defaults: This variable holds a default property list associated with a Properties object. 
Features of Properties class:
Properties is a subclass of Hashtable.
It is used to maintain a list of values in which the key is a string and the value is also a string i.e; it can be used to store and retrieve string type data from the properties file.
Properties class can specify other properties list as it’s the default. If a particular key property is not present in the original Properties list, the default properties will be searched.
Properties object does not require external synchronization and Multiple threads can share a single Properties object.
Also, it can be used to retrieve the properties of the system.
Advantage of a Properties file
In the event that any data is changed from the properties record, you don’t have to recompile the java class. It is utilized to store data that is to be changed habitually.
Note: The Properties class does not inherit the concept of a load factor from its superclass, Hashtable.
Declaration
public class Properties extends Hashtable<Object,​Object>
1. Properties(): This creates a Properties object that has no default values. 
Properties p = new Properties();
2. Properties(Properties propDefault): The second creates an object that uses propDefault for its default value. 
Properties p = new Properties(Properties propDefault);
Example 1: The below program shows how to use Properties class to get information from the properties file. 
Let us create a properties file and name it as db.properties.
db.properties
username = coder
password = geeksforgeeks
Code
Java
// Java program to demonstrate Properties class to get// information from the properties file  import java.util.*;import java.io.*;public class GFG {    public static void main(String[] args) throws Exception    {        // create a reader object on the properties file        FileReader reader = new FileReader("db.properties");          // create properties object        Properties p = new Properties();          // Add a wrapper around reader object        p.load(reader);          // access properties data        System.out.println(p.getProperty("username"));        System.out.println(p.getProperty("password"));    }}
Output
Example 2: The below program shows how to use the Properties class to get all the system properties. Using System.getProperties() method, we can get all the properties of the system.
Java
// Java program to demonstrate Properties class to get all// the system properties  import java.util.*;import java.io.*;  public class GFG {    public static void main(String[] args) throws Exception    {        // get all the system properties        Properties p = System.getProperties();          // stores set of properties information        Set set = p.entrySet();          // iterate over the set        Iterator itr = set.iterator();        while (itr.hasNext()) {              // print each property            Map.Entry entry = (Map.Entry)itr.next();            System.out.println(entry.getKey() + " = "                               + entry.getValue());        }    }}
Output
Example 3: The below program shows how to use the Properties class to create a properties file.
Java
// Java program to demonstrate Properties class to create// the properties file  import java.util.*;import java.io.*;  public class GFG {    public static void main(String[] args) throws Exception    {        // create an instance of Properties        Properties p = new Properties();          // add properties to it        p.setProperty("name", "Ganesh Chowdhary Sadanala");        p.setProperty("email",                      "ganeshs.gfg@gmail.com");          // store the properties to a file        p.store(new FileWriter("info.properties"),                "GeeksforGeeks Properties Example");    }}
Output
METHOD
DESCRIPTION
Deprecated.
This method does not throw an IOException if an I/O error occurs while saving the property list.
METHOD
DESCRIPTION
METHOD
DESCRIPTION
Ganeshchowdharysadanala
Java - util package
Java-Collections
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Initialize an ArrayList in Java
Object Oriented Programming (OOPs) Concept in Java
Interfaces in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
Singleton Class in Java
Stack Class in Java
Multithreading in Java