I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 years. I like video games and reading books.
1. Introduction to Java.Util.Properties Class
Most of the enterprise applications settings are actually loaded during the application startup itself and the application behavior is controlled by the application settings persisted in a Flat file or Registry or Database etc.
In this example, we are going to create application property file called "MyApp.Properties" and going store the application settings into that file. We will also read the persisted properties from that file and display that in the Console Window.
2. Key&Value Pairs of Properties
The "Properties Class" of Java is used to maintain one or more properties that can be easily streamed into Text or Binary. Each property is a Key & Value pair. Now, let us create three Property Values and store that in a Java's Properties object called AppProps. This example requires set of Java Packages and the code given below shows those imports:
Now look at the screenshot below:
Here, first, we are creating a Java Properties object called AppProps which will hold application properties (Marked as 1). Once the object is on hand, we are storing three properties by calling its "setProperty()" method.
The "setProperties()" method accepts two strings and forms Key-Value Pair. Later, the Value can be retrieved by providing the corresponding Key.
The two parameters passed to it is "Key and Value" pair. For example, the third property we are adding is "FontSize" and the Size of the font is 12. Here, "FontSize" is the Key (Marked as 2) which is passed as First Parameter and 12 is the value for it which is passed as second parameter (Marked as 3). So, in the code snippet, we created three application settings and stored that in a Properties object called AppProps.
Listing 1: Creating Application Settings
The “store()” method of the Java’s Properties Class Persists Key-Value pair to Disc and the “load()” method will read the persisted information from the disc and forms the Key-Value Pair
3. Storing Application Properties Using "Properties::store()" Method
The application properties contained in the Properties Class instance can be persisted to a text file. The “store()” method of the Properties Class is used to save the application properties to a text file. This method takes an OutputStream or Writer object to store the information. Since it accepts OutputStream as well as Writer, in place of a text file, one can write the properties in a binary file as well. The most preferred way is writing it to a text file and preferred extension for the property file is “.properties”. We can persist the information in an XML file also.
Now have a look at the Screenshot below:
First, we are getting Path to our “.properties file” by making use of the “static get() method” call of the Paths Utility Class (Marked as 1). A Write object PropWriter is then created by calling another utility function “newBufferedWriter()”. This function takes Path to our properties file (Marked as 2).
Recommended for You
Now, we have our Writer object and Path object are ready. We are making calls to the Store() method of the Properties class by supplying the Writer object to it (Passed as the first parameter, marked as 3). We are also passing the comment text “Application Properties” as the second parameter (Marked as 4) and this text appears as comment text in the output file.
Once the properties are written to the text file, the content looks as shown below:
The comment passed to the store method appears as the first line in the properties file (Marked as 1) and there are date and time stamp (marked as 2) those tell when the properties are persisted. As these two lines are comment lines, we can see # is prefixed. The actual properties are persisted as “Key & Value” pairs which are marked as 3 in the above screenshot. Note that the default format of a single property is “Key=Value”.
We can also hand-code and create the properties file. Follow the below guidelines:
- Key and Value pairs can be created one per line.
- Use the “=” or “:” as a separator between Key & Value.
- To have = or: in key and/or value, use the escape char \.
- To place comment, prefix the line with # or ! symbol.
- To organize a group of properties use comment heading and a blank line at the end of the group.
Listing 2: Writing the Properties to Text File
4. Loading Properties from Text File Using "Properties::load()" Method
We used "Writer Text Stream" for storing the Application settings in the properties file. Now, we are going to use "Reader Stream" to read the Property settings from the file. Once the properties are read from the “.Properties” to Java’s “Properties Class” instance, we will display the property settings in the Console Output Window. Below is the code snippet for this:
First, we are creating the "Reader" instance PropReader by making use of the "newBufferedReader()" method (Marked as 1). Note that we are reusing the PropertyFile instance which we used for writing the application properties. Most of the Time, the property files are created manually and we can use this same approach to read the file.
We are using the “load() method” of the Properties Class to load the Properties stored in the MyApp.Properties file through the passed-in Reader object called PropReader (Marked as 2). After "load()" call, we have the all the property settings loaded into Properties Class instance called AppProps.
The "getProperty()" method of Properties Class takes the Key and returns the Value associated to that Key. In our example, we are calling this method three times and printing the returned result in the Console Output Window (Marked as 3 – 6). Below is the Complete code Example and Its Output.
Reading and Writing Java Property File - Complete Code Example
Output of the Code Example
The Java Programmers usually pick ".Properties" as file extension which persists the Java Properties to a Text file. We saw the usage of the store() and load() methods of the Java's "Properties Class" and how it stores and retrieves the application properties from the ".properties" file. Since the Java ".Properties" files are usually ASCII Standard text files we used Java's Reader and Writer objects.
In this example, we saw Properties persisted as a text file. The Java's Properties class supports storing and retrieving the data from XML File as well through APIs "loadFromXml()" and "storeToXML()".