In this article, we will discuss Properties class – one of the Map implemented classes through Java Hashtable class in detail
1. Key points about Properties:
- Properties class is a legacy class
- to store key-value pairs,
- where both keys & values are of strictly String data-type
2. Properties:
- Java Properties class is sub-class of Hashtable class (i.e.; Properties extends Hashtable)
- It is used to store key-value pairs where both Key/Value is String type
- This is mainly used to store those kinds of data, which changes periodically/frequently like database connection details, etc.
- Reason: if we hard-code these things into Java file, then necessary re-compilation & re-package & re-deploy is required (sometime server restart is also required)
- Using Java Properties, we can overcome this situation by simply changing values in properties file & re-deploying into server
- Java Properties is used to fetch/read environment variables using getProperties() method
- Present in java.util package and extends java.util.Hashtable class
- Also, implements java.lang.Cloneable, java.io.Serializable marker interfaces (through super class Java Hashtable class) which provides special ability to Java Properties class (provided by JVM at run time) like,
- java.lang.Cloneable: to create a duplicate object or to clone an object
- java.io.Serializable: to transfer objects across network
Source: Team BenchResources.Net
3. Properties constructors:
3.1 Properties prop = new Properties();
- creates an empty Properties object
- which has no default values
3.2 Properties prop = new Properties(Properties props);
- creates an empty Properties object
- with specified default values
4. Java Properties method:
- Properties specific methods from Java 1.0 version
Java Properties method | Description |
String getProperty(String propKey); | to read/get value associated with the specified property-key
returns null, if specified key not in the property list |
Object setProperty(String propKey, String propValue); | used to set key-value pair in properties file
if key is already present, then value is overridden otherwise, null is returned |
Enumeration propertyName(); | returns enumeration of keys from property list
or to get all properties |
void load(InputStream is); | to load properties from (.properties) file to Java properties object |
void store(OutputStream os, String comments); | to store/save properties from Java properties object into (.properties) file |
5. Properties examples:
5.1 To store or save to properties file
PropertiesStoreInFile.java
package in.bench.resources.java.collection;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
public class PropertiesStoreInFile {
public static void main(String[] args) {
// creating Properties object to store db credentials
Properties prop = new Properties();
// writing db credentials as key-value pairs
prop.setProperty("db.url", "localhost");
prop.setProperty("db.port", "1521");
prop.setProperty("db.name", "xe");
prop.setProperty("db.username", "tiger");
prop.setProperty("db.password", "scout");
try {
// write and save
OutputStream os = new FileOutputStream("db.properties");
prop.store(os, "Storing DB Credentials");
System.out.println("Saved to db.properties file");
}
catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
}
catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
Output:
#Storing DB Credentials
#Fri Aug 26 17:02:08 IST 2016
db.password=scout
db.name=xe
db.username=tiger
db.port=1521
db.url=localhost
5.2 To read from properties file
PropertiesLoadAndReadFromFile.java
package in.bench.resources.java.collection;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesLoadAndReadFromFile {
public static void main(String[] args) {
// creating Properties object to load/read db credentials
Properties prop = new Properties();
try {
// reading file as stream
InputStream is = new FileInputStream("db.properties");
// load db.properties file
prop.load(is);
// printing DB credentials values
System.out.println("Database URL or host : "
+ prop.getProperty("db.url"));
System.out.println("Database port : "
+ prop.getProperty("db.port"));
System.out.println("Database name : "
+ prop.getProperty("db.name"));
System.out.println("Database username : "
+ prop.getProperty("db.username"));
System.out.println("Database password : "
+ prop.getProperty("db.password"));
}
catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
}
catch (IOException ioex) {
ioex.printStackTrace();
}
}
}
Output:
Database URL or host : localhost
Database port : 1521
Database name : xe
Database username : tiger
Database password : scout
Related Articles:
- Map interface
- Entry interface
- HashMap class
- LinkedHashMap class
- IdentityHashMap class
- WeakHashMap class
- SortedMap interface
- NavigableMap interface
- TreeMap class
- Hashtable class
- HashMap vs LinkedHashMap
- HashMap v/s LinkedHashMap v/s TreeMap
- HashMap v/s HashSet
- HashMap v/s Hashtable
- Properties class
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
- https://docs.oracle.com/javase/tutorial/essential/environment/properties.html
- https://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html
- https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html
Happy Coding !!
Happy Learning !!