22-12-2019 - Shai Zambrovski
Must of Java application need to use with key/value pairs for persist configuration metadata, user information or any data that related to a program or application. So we need to have am intuitive way to manage key/value mechanism to:
for this, Java provides us the java.util.Properties
class that manage the key/value pair as a String.
The Properties
class provides us basically two methods:
setPropertySet
value (create or modify) to a specific key.getPropertyGet
the value of a specific key; null
is returned if key doesn’t exists, unless we provide default value.So how we can persist those properties?
Using streams we can basically persist two types of properties files format; .properties
and .xml
files.
.properties
formatoutputStream
& inputStream
(if we work with bytes) or reader
& writer
(if we work with characters).:
or =.
#
or !
..properties
file:Properties props = new Properties();
props.setProperty("courseName", "Java Fundamentals");
props.setProperty("courseLocation", "Auditorium");
try (Writer writer = Files.newBufferedWriter(Paths.get("courses.properties"))) {
props.store(writer, "2019 courses"); // In case we don't want any comments, need to pass a null in the 2nd argument
}
courses.properties
file:
#2019 courses
#Wed Dec 25 14:03:57 IST 2019
courseLocation=Auditorium
courseName=Java Fundamentals
.properties
file:Properties props = new Properties();
try (Reader reader = Files.newBufferedReader(Paths.get("courses.properties"))) {
props.load(reader);
} catch(...) {}
String name = props.getProperty("courseName"); //Java Fundamentals
String location = props.getProperty("courseLocation"); //Auditorium
.xml
formatoutputStream
& inputStream
..xml
file:Properties props = new Properties();
props.setProperty("courseName", "Java Fundamentals");
props.setProperty("courseLocation", "Auditorium");
try (OutputStream os = Files.newOutputStream(Paths.get("courses.xml"))) {
props.storeToXML(os, "2019 courses"); // In case we don't want any comments, need to pass a null in the 2nd argument
}
courses.xml
file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>2019 courses</comment>
<entry key="courseLocation">Auditorium</entry>
<entry key="courseName">Java Fundamentals</entry>
</properties>
.xml
file:Properties props = new Properties();
try (InputStream is = Files.newInputStream(Paths.get("courses.xml"))) {
props.loadFromXML(is);
} catch (...) {}
String name = props.getProperty("courseName"); //Java Fundamentals
String location = props.getProperty("courseLocation"); //Auditorium
In some cases and scenarios we want to have a default values to provide an abstraction for some configurations.
To achieve such feature, we can pass to the Properties
constructor a Properties
instance that will supply a default values for a keys that doesn’t exists in the current Properties
instance.
Properties defaultProps = new Properties();
defaultProps.setProperty("courseLocation", "classroom A33");
Properties configuration = new Properties(defaultProps);
String location = configuration.getProperty("courseLocation"); // "classroom A33"
configuration.setProperty("courseLocation", "Auditorium"); // Override default value
location = configuration.getProperty("courseLocation"); // "Auditorium"