"Today we are exploring another new and interesting topic: properties."

"In Java, it's customary to make programs flexible and easily customizable, i.e. easily configurable."

"For example, once per hour your program copies files from a certain directory, zips them, and sends them to you in email. To do this, the program needs to know the directory where the files will be taken from, and the email address where they should be sent. Such data is usually stored not in application code, but in a separate properties files."

The data in this file is stored as key-value pairs, separated by an equal sign.

Example
data.properties file
directory = c:/text/downloads
email = zapp@codegym.cc

"To the left of the sign is the name (key), to the right is the value."

"So this is a kind of textual representation of a HashMap?"

"In general, yes."

"For convenience in working with such files, Java has a special Properties class. The Properties class inherits Hashtable<Object,Object>. It can even be thought of as a Hashtable that can load itself from a file."

"Here are its methods:"

Method Description
void load(Reader reader) Loads properties from the file represented by a Reader object
void load(InputStream inStream) Loads properties from the file represented by an InputStream object
void loadFromXML(InputStream in) Load properties from an XML file
Object get(Object key) Returns the value for the specified key. This method is inherited from Hashtable
String getProperty(String key) Returns a property value (string) by key.
String getProperty(String key, String defaultValue) Returns a property value by key or defaultValue if there is no such key.
Set<String> stringPropertyNames() Returns a list of all keys

"In other words, you actually need to perform only two operations— load data from some file into a Properties object and then get these properties using the getProperty() method. Well, and don't forget that you can use the Properties object like a HashMap."

"Here's another example:"

Code
// The file that stores our project's properties
File file = new File("c:/data.properties");

// Create a Properties object and load data from the file into it.
Properties properties = new Properties();
properties.load(new FileReader(file));

// Get property values from the Properties object
String email = properties.getProperty("email");
String directory = properties.getProperty("directory");

// Get a numeric value from the Properties object
int maxFileSize = Integer.parseInt(properties.getProperty("max.size", "10000"));

"Ah. So we create a Properties object, and then pass a file to it. To the load method. And then we just call getProperty. Right?"

"Yep."

"And you also said that it can be used as a HashMap? What did you mean?"

"The Properties class inherits Hashtable, which is the same as HashMap, but all its methods are synchronized. You can simply display all the values ​​from the properties file on the screen like this:"

Code
// Get the file with the properties
File file = new File("c:/data.properties");

// Create a Properties object and load data from the file into it.
Properties properties = new Properties();
properties.load(new FileReader(file));

// Run through all the keys and display their values on the console
for (String key : properties.stringPropertyNames())
{
 System.out.println(properties.get(key));
}

"Ah. Everything seems to have fallen into place. Thank you, Rishi. I'm going to use this cool thing."