read and write key/value in .properties file

.properties is a file extension for files mainly used in Java related technologies to store the configurable parameters of an application. the parameters are in terms of key value pair, means we can use the key in the code to get the value stored in the .properties file.

This basically helps to avoid going to the code for any change needed in the value, we can directly change in the file itself.
An example: Open a notepad, enter key and values and "Save As" - "config.properties"

Usage:
Normally in framework, we use .properties file to store Username, Password, URL under test, browser type / version, platform etc...
it helps each automation tester to use their own details in program and also helps to keep their credentials confidential. 

Let's understand more about the .properties file!
Format:
  • [key] = [value] or [key] : [value]
  • For comments, statements starts with either ! or #.
  • White space at beginning or before and after of = or : are ignored.
  • Blank lines are ignored.
  • If value is written in multi line, then each line should be ended with '\' (backslash).
Let's see the code to read the values(MethodName-getVal()) from the key/variable we entered in the .properties file and how to update/write values to the keys(MethodName-setVal()).
package testPkg;

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

public class PropertiesFile {
 public static String getVal(String key)
  {
   String keyval = null;
   File file = new File("./config.properties");
                        //Let's consider properties file is in project folder itself
   //Creating properties object
   Properties prop = new Properties();     
   //Creating InputStream object to read data
   FileInputStream objInput = null;
   try{
     objInput = new FileInputStream(file);
     //Reading properties key/values in file
     prop.load(objInput);
     keyval = prop.getProperty(key);
     objInput.close();
   }catch(Exception e){System.out.println(e.getMessage());}
   return keyval;
  }

 public static void setVal(String key, String keyval)
  {
      File file = new File("./config.properties");
     Properties prop = new Properties();
     FileInputStream objInput = null;
     try {
      objInput = new FileInputStream(file);
      prop.load(objInput);
      objInput.close();

      FileOutputStream out = new FileOutputStream("./config.properties");
   prop.setProperty(key, keyval);  
   prop.store(out, null);
   out.close();
      } catch (Exception e) {System.out.println(e.getMessage());}   
  }

 public static void main(String[] args) {
  System.out.println("username:"+getVal("username"));
  System.out.println("passowrd:"+getVal("password"));
  setVal("username", "JohnConer");
  System.out.println("After updating properties file");
  System.out.println("username:"+getVal("username"));
  System.out.println("passowrd:"+getVal("password"));
 }
}
Out Put:

No comments:

Post a Comment