How to Create and Modify Properties File Form Java?


Program in Text and XML Format?

In Java, a project's properties file is made up of text-based key-value pairs that are commonly stored with the .properties extension. The key-value pair of contents are presented line by line, and they are often created using notepad, Wordpad, etc.

Properties files serve as valuable repositories for storing critical and confidential data. In this article, we will explore the process of creating a properties file using a Java program. The Properties class in this (java.util.Properties) package offers multiple utility store methods that facilitate the storage of properties in either

  • Text Format or

  • XML Format

Text Format Creation

The store() function is used to save properties in text format. This function saves the key-value pairs from the properties table to an OutputStream in a manner that can easily be loaded into a properties table using the load method, which accepts an InputStream.

Declaration

//declaration for store() method
public void store(OutputStream out,String comments)

Parameters

  • Out refers to the Output Stream utilized for generating the XML document.

  • Comments serve as an explanatory text for the property list.

Return Value

This method retrieves the value from the property list associated with the specified key, returning the previous value if it existed, or null if it did not.

Example

As we are creating properties file in a text format a “valid file path location should be given”.

import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.io.FileOutputStream;

public class TutorialsPoint {

   public static void main(String args[])
   throws IOException,FileNotFoundException{

      // Firstly we will create properties files
      Properties prop = new Properties();

      //Testing.properties file is created in the current directory
      FileOutputStream fileOutputStream
      = new FileOutputStream(
      "Testing.properties");

      //setting the values to the properties
      //like username and password
      prop.setProperty("username", "Tutorials Point");
      prop.setProperty("password", "Success");

      //we are using store() method as we are
      //storing properties into properties file in text format
      prop.store(
      fileOutputStream,
      "Creating Properties file in Text Format");

      fileOutputStream.close();
   }
}

Output

We will see a file that is created at a specified location that we have mentioned  in the program. If path is not specified only file name is given then it downloaded in current working directory

When we open the file and we will see the following content

#Creating Properties file in Text Format
#Sun May 28 15:58:54 IST 2023
password=Success
username=Tutorials Point

Other than key-value pairs all are considered are comments represented by # symbol.

Utilizing properties in Text Format is advantageous when the contents of the properties are minimal.

XML Format Creation

XML is an efficient format for storing important sensitive information. Extensible Markup Language(XML) has a set of rules for encoding documents. In order to store properties in XML format then we need to use storeToXML() method.

The storeToXML() method produces an XML document that represents all the properties present in this table.

Declaration

//declaration for storeToXML() method
public void storeToXML(OutputStream os,String comment)

Parameters

  • Out refers to the Output Stream utilized for generating the XML document.

  • Comments serve as an explanatory text for the property list.

Return Value

As storeToXML() method return type is void so it will not return any value.

Example

As we are creating properties file in a text format a “valid file path location should be given”.

import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Properties;
import java.io.FileOutputStream;

public class TutorialsPoint {

   public static void main(String args[])
   throws IOException, FileNotFoundException{

      // we will create properties files
      Properties prop = new Properties();

      //Testing.properties is created in the current directory
      FileOutputStream fileOutputStream
      = new FileOutputStream(
      "Testing.xml");

      //setting the values to the properties
      //like username and password
      prop.setProperty("username", "Tutorials Point");
      prop.setProperty("password", "Success");

      //we are using storeToXML() method as we are
      //storing properties into properties file in XML format
      prop.storeToXML(
      fileOutputStream,
      "Creating Properties file in XML Format");

      fileOutputStream.close();
   }
}

Output

We will see a file that is created at a specified location that we have mentioned in the program. If path is not specified only file name is given then it downloaded in current working directory

When we open the file and we will see the following content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Creating Properties file in XML Format</comment>
<entry key="password">Success</entry>
<entry key="username">Tutorials Point</entry>
</properties>

When the properties file contains extensive and crucial information, it is advisable to exclusively opt for the XML format.

Sometimes it is difficult for us to read in XML format so we had a way to convert XML contents to read-only mode. The java program below helps us with this requirement.

Steps to Follow

  • Input XML file which contains necessary information.

  • Select a destination to store the output that is in text format.

  • Load XmL file using loadFromXmL().

  • Finally we will get the output in the text format that we have seen above.

Input File(Testing.xml)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Creating Properties file in XML Format</comment>
<entry key="password">Success</entry>
<entry key="username">Tutorials Point</entry>
</properties>

Example

import java.io.FileInputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TutorialsPoint {
   public static void main(String[] args)
   throws InvalidPropertiesFormatException, IOException{
      String resultantFile
      = "sample.properties";
      String inputFile
      = "Testing.xml";

      InputStream is
      = new FileInputStream(inputFile);

      OutputStream os
      = new FileOutputStream(resultantFile);

      Properties properties = new Properties();
      properties.loadFromXML(is);

      properties.store(
      os,
      "Converted from Testing.xml");
   }
}

Output

When we open the file and we will see the following content

#Converted from Testing.xml
#Sun May 28 16:46:03 IST 2023
password=Success
username=Tutorials Point

Conclusion

In this article we looked at how to build properties files and we have also seen a way to convert XML contents to read-only mode. The Usage of dynamic elements is evident, allowing for effortless modifications at any given time.

Updated on: 16-Oct-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements