Sunday, 17 May 2020

How to read properties file and fetch data? Write code to read common.properties file?


  • In below code filepath parameter will contain the path where we will keep the properties file and key parameter is the name of the value which we want fetch.
  • public is the access modifier
  • static  so that we can access the class without creating object
  • String is the return type of propertyReader function

============================
public static String propertyReader(String filePath,String key)

{
        String value = null;
       
        //Inputstream is required while loading into properties
   
       try (InputStream input = new FileInputStream(filePath))
        {
           
           // object creation for Property class
            Properties prop = new Properties();

            // load a properties file
            prop.load(input);

            //getProperty will fetch the value according to the key
            value=prop.getProperty(key);
         

        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return value;
       
    }


Want to learn more about Framework Design, do check out below link :



 

************************************************* 

API Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Rest-API

 
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes

 
Docker Interview Question Set
https://automationreinvented.blogspot.com/2020/02/top-18-docker-commands-for-aytomation.html

 
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux

 
Automation Testing/SDET Framework Design 
https://automationreinvented.blogspot.com/search/label/FrameworkDesign


Java Related Interview Question Set

https://automationreinvented.blogspot.com/search/label/Java


GIT Interview Question Set:
https://automationreinvented.blogspot.com/search/label/GIT


Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions

***********************************
Note: If anyone need help in designing framework or virtual support for Automation work then reach out to me @sidharth.shukla19@gmail.com/9619094122
***********************************
For training check the Training link as below:
Training Details
More content for Interview preparation:
How to add execution status to Extent Report

3 comments:

  1. what is best case to use properties file in industry

    ReplyDelete
    Replies
    1. project settings and configuration data with database configuration

      Delete
  2. This comment has been removed by the author.

    ReplyDelete

All Time Popular Posts

Most Featured Post

Introduction to Web Testing with Playwright

  Introduction to Web Testing with Playwright 📌  Telegram Group:  https://t.me/+FTf_NPb--GQ2ODhl 📌  YouTube channel:  https://lnkd.in/gGUG...