Java - Read/Write Properties file

Having a Properties file in any Java application in one of the mandatory things which needs to be done in order to achieve generic functionality. The reason behind this statement is that if any constant values are placed in a Final Java class, then if the value needs to be changed at a later time, then it involves changing the source code of the class which would result in re-deployment of the application.

In order to make it simpler, if the properties file have those constant values then changing the Properties file alone would achieve the functionality. Its so simple right!

In the below section, we are going to describe the format of the properties file and how the property file is loaded and modified at run time.

Properties File:

Name=TechDive
Age=23
Note: Place the above text in file C:/Src.prop

HandlePropertyFile Class

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package in.techdive.java.examples;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * This class details the Usage of Property file read and write operation.
 */

public class HandlePropertyFile
{
  public static void main(String[] args) throws Exception
  {
    //Read properties file and display values
    Properties properties = readPropertyFile();
    if (properties != null)
    {
      System.out.println("Properties Object = " + properties);
      System.out.println("Name = " + properties.getProperty("Name"));
      System.out.println("Age = " + properties.getProperty("Age"));

      //Update Properties Object and update the Properties file.
      properties.put("Name", "Ram");
      properties.put("Age", "25");
      writePropertyFile(properties);
    }
  }

  /**
   * Overwrite the Properties file with the updated Properties object
   *
   * @param p_prop
   * @throws Exception
   */

  private static void writePropertyFile(Properties p_prop) throws Exception
  {
    FileOutputStream fos = null;
    try
    {
      fos = new FileOutputStream("C:/Src.prop");
      p_prop.store(fos, "Properties file updated");
    }
    catch (Exception e)
    {
      System.err.println("Error in writing Property file.");
      throw new Exception();
    }
  }

  /**
   * Read Properties file from the location C:/Src.prop
   *
   * @return Properties
   * @exception
   */

  public static Properties readPropertyFile() throws Exception
  {
    FileInputStream fis = null;
    Properties prop = null;
    try
    {
      fis = new FileInputStream("C:/Src.prop");
      if (fis != null)
      {
        prop = new Properties();
        prop.load(fis);
      }
      else
      {
        //Error in reading Property file
        throw new Exception();
      }
    }
    catch (Exception e)
    {
      System.err.println("Error in reading Property file. Exception Message = " + e.getMessage());
      prop = null;
    }

    finally
    {
      if (fis != null)
      {
        try
        {
          fis.close();
        }
        catch (Exception ie)
        {
          System.err.println("Error in closing the File Input Stream; Exception Message = " + ie.getMessage());
        }
      }
    }
    return prop;
  }
}

Read Properties:
The above code will read the Property file from the location C:/Src.prop and display the value as below,

Output:

Properties Object = {Name=TechDive, Age=23}
Name = TechDive
Age = 23

Write Properties:
The value in the properties object which were loaded earlier were modified in the properties object and written in the Src.prop file.

The File will be updated with the new values.

Updated Src.prop file:

#Properties file updated
Name=Ram
Age=25
Technology: 

Search