Thursday, September 5, 2013

How to Read and Write to a Property File In Java -Simple example

In java.util package there is a class called Properties which easily allow you to do read and write operations with Property files. The get() method reads from the file and the store() method writes to the file. If the property is already available in the file, the value will be overridden. Using this class is very simple.

First of all you have to initialize the Properties object by loading a ".properties" file.

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

public class PropertyTest {
  private static Properties properties;
  private static File propertyFile;

  public PropertyTest() {
    initProperties();
  }

  private static void initProperties() {
    try {
      propertyFile = new File("D:/test.properties");
      if (!propertyFile.exists()) {
        propertyFile.createNewFile();
      }
      FileInputStream in = new FileInputStream(propertyFile);
      properties = new Properties();
      properties.load(in);
      in.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

Note the properties.load() method in above code. That method loads the existing properties to properties Object.

Now your properties object is ready to use. You can save or load data now.
Below code add a property called "testProperty" to the file.
  private static void addProperty() {
    try {
      FileOutputStream out = new FileOutputStream(propertyFile);
      properties.setProperty("testProperty", "This is test property");
      properties.store(out, "This is comment");
      out.close();
    } catch (Exception ex) {
    }
  }


Note that we have used the properties.store() method to save the value.

In below method you can see how to retrieve data from file.
  private static void readProperty() {
    String testProperty = (String) properties.get("testProperty");
    System.out.println(testProperty);
  }

Note that if the property does not exist in the file it returns null.
That is all.

The following is a full working code.
package testing;

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

public class PropertyTest {
  private static Properties properties;
  private static File propertyFile;
 
  public PropertyTest() {
    initProperties();
  }
 
  public static void main(String[] args) {
    initProperties();
    addProperty();
    readProperty();
  }
 
  private static void addProperty() {
    try {
      FileOutputStream out = new FileOutputStream(propertyFile);
      properties.setProperty("testProperty", "This is test property");
      properties.store(out, "This is comment");
      out.close();
    } catch (Exception ex) {
    }
  }

  private static void readProperty() {
    String testProperty = (String) properties.get("testProperty");
    System.out.println(testProperty);
  }
 
 
  private static void initProperties() {
    try {
      propertyFile = new File("D:/test.properties");
      if (!propertyFile.exists()) {
        propertyFile.createNewFile();
      }
      FileInputStream in = new FileInputStream(propertyFile);
      properties = new Properties();
      properties.load(in);
      in.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

No comments:

Post a Comment