Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
November 27, 2019

How to Read Data From Properties File in Java




In this blog, we will see How to Read Data From Properties File in Java. This will be asked in the most of the interview questions.

Things to Remember:

  1. Mostly, the properties file is used to maintain the configurable parameters of the project.
  2. Extension for this file is “.properties”
  3. Properties file data will be in a Key and Value pair format.
  4. whenever you’re creating a properties file, maintain the keys in unique format. Don’t use the existing key for your new value.
    • If you use the existing key again, then whenever control searches for that key it will return the last searched value. This might leads to conflicts.
#CONFIG File
username = Username
password = pass
firstname = fname





In the below snippet, we have done the following to Read Data from properties file:

  1. Created a new folder under our project and created  a new properties file with some data
  2. Created a object and assigned the file location of this newly created properties file using FileInputStream class.
  3. Created a object for the Properties class.
  4. Loaded all the properties of our file using Properties.load() method.
  5. Using .getProperty() method, we can load all our data using Key as a parameter.
  6. Then, it prints the corresponding value of that key.
package Javaprograms;

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

public class ReadDataFromPropertiesFile {

	public static void main(String[] args) throws Exception {
		FileInputStream fis = new FileInputStream("E:/Selenium/SeleniumPrgrms/resources/config.properties");
		Properties prop = new Properties();
		
		prop.load(fis);
		
		String user = prop.getProperty("username");
		String pword = prop.getProperty("password");
		String fname = prop.getProperty("firstname");
		
		System.out.println(user);
		System.out.println(pword);
		System.out.println(fname);
	}

}
Output:
--------
username = Username
password = pass
firstname = fname





Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook on Google+

How to Compare Two Arrays in Java without built-in functions How to SWAP Two Numbers in Java using Temp Variable

Related Posts

How To SWAP Two Numbers in Java Without using Temp Variable

Java IQs

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Compare two arrays in java

Java IQs

How to Compare Two Arrays in Java using built-in functions

Remove White Spaces From A String in Java

Java IQs

How to Remove Whitespaces from a String in Java

Sum of The digits in a number in Java

Java IQs

How to Perform Sum of the Digits in a Given Number in Java

How To Reverse A String in Java

Java IQs

How to Reverse a String in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2023