The properties object consists of string key and value pairs. Within the Java programming language, the java.util.Properties class serves as a subclass of Hashtable.
The Properties class enables retrieving property values by their corresponding keys. This class offers functionalities for extracting information from properties files and saving data back into them. Additionally, it supports retrieving system properties.
An Advantage of the properties file
Recompilation is unnecessary when altering details in a properties file: Modifying information in a properties file does not necessitate recompiling the Java class. Properties files are ideal for storing data that requires frequent updates.
Constructors of Properties class
| Method | Description |
|---|---|
| Properties() | It creates an empty property list with no default values. |
| Properties(Properties defaults) | It creates an empty property list with the specified defaults. |
Methods of Properties class
Listed below are the frequently utilized techniques of the Properties class:
| Method | Description |
|---|---|
| public void load(Reader r) | It loads data from the Reader object. |
| public void load(InputStream is) | It loads data from the InputStream object |
| public void loadFromXML(InputStream in) | It is used to load all of the properties represented by the XML document on the specified input stream into this properties table. |
| public String getProperty(String key) | It returns value based on the key. |
| public String getProperty(String key, String defaultValue) | It searches for the property with the specified key. |
| public void setProperty(String key, String value) | It calls the put method of Hashtable. |
| public void list(PrintStream out) | It is used to print the property list out to the specified output stream. |
| public void list(PrintWriter out)) | It is used to print the property list out to the specified output stream. |
| public Enumeration<?> propertyNames()) | It returns an enumeration of all the keys from the property list. |
| public Set<String> stringPropertyNames() | It returns a set of keys in from property list where the key and its corresponding value are strings. |
| public void store(Writer w, String comment) | It writes the properties in the writer object. |
| public void store(OutputStream os, String comment) | It writes the properties in the OutputStream object. |
| public void storeToXML(OutputStream os, String comment) | It writes the properties in the writer object for generating XML document. |
| public void storeToXML(Writer w, String comment, String encoding) | It writes the properties in the writer object for generating XML document with the specified encoding. |
Example of Properties class to get information from the properties file
Begin by generating the properties file in order to retrieve data from it.
user=system
password=oracle
Next, we will proceed with developing the Java class responsible for retrieving data from the properties file.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
FileReader reader=new FileReader("db.properties");
Properties p=new Properties();
p.load(reader);
System.out.println(p.getProperty("user"));
System.out.println(p.getProperty("password"));
}
}
Output:
system
oracle
By updating the properties file, there is no necessity to recompile the Java class, ensuring there are no maintenance issues.
Example of Properties class to get all the system properties
Through the utilization of the System.getProperties method, we are able to retrieve all the properties associated with the system. Now, let's proceed by crafting a class that will extract details from the system properties.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
Properties p=System.getProperties();
Set set=p.entrySet();
Iterator itr=set.iterator();
while(itr.hasNext()){
Map.Entry entry=(Map.Entry)itr.next();
System.out.println(entry.getKey()+" = "+entry.getValue());
}
}
}
Output:
java.runtime.name = Java(TM) SE Runtime Environment
sun.boot.library.path = C:\Program Files\Java\jdk1.7.0_01\jre\bin
java.vm.version = 21.1-b02
java.vm.vendor = Oracle Corporation
java.vendor.url = http://java.oracle.com/
path.separator = ;
java.vm.name = Java HotSpot(TM) Client VM
file.encoding.pkg = sun.io
user.country = US
user.script =
sun.java.launcher = SUN_STANDARD
...........
Example of Properties class to create the properties file
Next, we will proceed with writing the code necessary for generating the properties file.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args)throws Exception{
Properties p=new Properties();
p.setProperty("name","Sonoo Jaiswal");
p.setProperty("email","sonoojaiswal@example.com");
p.store(new FileWriter("info.properties"),"C# Tutorial Properties Example");
}
}
Let's see the generated properties file.