The Text Java Properties format parses any Java properties file. This format represents simple key-value pairs. DataWeave represents these pairs as an object with string values.
MIME Type: text/x-java-properties
ID: properties
This example shows how DataWeave represents a properties file.
The following text/x-java-properties
data is from a properties file. The file contains key-value pairs that provide host and port values. This content serves as the input payload to the DataWeaave script.
host=localhost port=1234
The DataWeave script transforms the text/x-java-properties
input payload to the DataWeave (dw) format and MIME type. It returns a string.
%dw 2.0 output application/dw --- payload
The output is an object in the DataWeave (dw) format. The object contains a collection of key-value pairs that match the text/x-java-properties
input. Notice that the output wraps the values in quotation marks.
{ host: "localhost", port: "1234" }
DataWeave supports the following configuration properties for the Text Java Properties format.
There are no reader properties for this format.
The Text Java Properties format accepts properties that provide instructions for writing output data.
Parameter | Type | Default | Description |
---|---|---|---|
bufferSize | Number | 8192 | Size of the writer buffer. |
deferred | Boolean | false | When set to true , DataWeave generates the output as a data stream, and the script’s execution is deferred until it is consumed. Valid values are true or false . |
encoding | String | null | Encoding for the writer to use. |
The Java Properties format supports the following MIME types.
MIME Type |
---|
*/x-java-properties |
*/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:
//Sample 01: Package inclusion import java.io.IOException; import java.nio.file.Files; import java.util.Properties; import java.nio.file.Path; import java.nio.file.Paths; import java.io.Writer; import java.io.Reader;
Now look at the screenshot below:
Adding Java Property to Properties Instance
Author
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.
//Example 01: Create List of Property Values Properties AppProps = new Properties(); AppProps.setProperty("Backcolor", "White"); AppProps.setProperty("Forecolor", "Blue"); AppProps.setProperty("FontSize", "12");
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
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:
Persisting Properties to Text File using Store() method
Author
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).
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:
Content of MyApp Properties File
Author
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”.
//Example 02: Store Properties to MyApp.Properties Path PropertyFile = Paths.get("MyApp.Properties"); try { Writer PropWriter = Files.newBufferedWriter(PropertyFile); AppProps.store(PropWriter, "Application Properties"); PropWriter.close(); } catch(IOException Ex) { System.out.println("IO Exception :" + Ex.getMessage()); }
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:
Reading Java Properties From Text File
Author
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.
//Sample 01: Package inclusion import java.io.IOException; import java.nio.file.Files; import java.util.Properties; import java.nio.file.Path; import java.nio.file.Paths; import java.io.Writer; import java.io.Reader; public class Main { public static void main(String[] args) { //Example 01: Create List of Property Values Properties AppProps = new Properties(); AppProps.setProperty("Backcolor", "White"); AppProps.setProperty("Forecolor", "Blue"); AppProps.setProperty("FontSize", "12"); //Example 02: Store Properties to MyApp.Properties Path PropertyFile = Paths.get("MyApp.Properties"); try { Writer PropWriter = Files.newBufferedWriter(PropertyFile); AppProps.store(PropWriter, "Application Properties"); PropWriter.close(); } catch(IOException Ex) { System.out.println("IO Exception :" + Ex.getMessage()); } //Example 03: Load Properties from MyApp.Properties try { //3.1 Load properties from File to Property // object Reader PropReader = Files.newBufferedReader(PropertyFile); AppProps.load(PropReader); //3.2 Read Property and Display it in Console System.out.println("Application BackColor:" + AppProps.getProperty("Backcolor")); System.out.println("Application ForeColor:" + AppProps.getProperty("Forecolor")); System.out.println("Application Font Size:" + AppProps.getProperty("FontSize")); //3.3 Close the Reader File PropReader.close(); } catch(IOException Ex) { System.out.println("IO Exception :" + Ex.getMessage()); } } }
If you like my post please follow me to read my latest post on programming and technology.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…