All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Read Properties File In Java

Last Updated : Mar 11, 2024

How To Read Properties File In Java

In this article we will show you the solution of how to read properties file in java, many user operations must be carried out and a lot of input data is needed to perform the testing when automating a web application.

These same properties file is advised to store this same input data as well as provide only the variable's reference to the program because hardcoding so much input data can occasionally make the code unusable and take longer to change in all of the program's fields.

Modifying this same input testing data inside the properties document is therefore sufficient. It has the ability to determine the value of a property based on its key.

Let's look at the property files:

  • The properties file contains everything in key-value pairs.
  • Therefore, we may retrieve any value in this file using the key.

The characters are written as the contents of the file. Four types of exceptions can be thrown, and it returns the file path.

Files with short content are better suited for this method. Path is another class used to link the filename to the path where the content will be stored.

The Files class also has a method called readString() which reads the content of any existing file and is used in the code to check if the content has been written correctly.

Step By Step Guide On How To Read Properties File In Java :-

import org.testng.annotations.Test;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.chrome.ChromeDriver;
public class TalkersCode{
    @Test
   public void talkerscode() throws IOException, InterruptedException {
        FileReader reader=new FileReader("C:\\Users\\ADMIN\\src\\main\\java \\TestData.properties");
        Properties props=new Properties();
        props.load(reader);
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\ADMIN\\Documents\\Selenium\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(props.getProperty("searchTerm"));
        Thread.sleep(2000);
        driver.close();
    }
}
  1. Open the web browser
  2. putting in the URL using the properties file's input.
  3. Make a properties file in the same package after you've finished customising the project, such as (TestData.properties).
  4. Right-click the package and choose >New>others file, then choose General and select a file to develop the properties file.
  5. Then, with the properties type selected, click Next because type the file name.

Conclusion :-

The Properties class includes steps for reading data from and writing data to the properties file. It can also be used to obtain system properties.

An asset file is a text file provided by Java that is used to store input data in programming.

The data in the properties document is recorded as Key-Value combinations, so we can use the key to link any data in this file.

I hope this article on how to read properties file in java helps you and the steps and method mentioned above are easy to follow and implement.