How to define file path in global.properties file?

1.8k Views Asked by At

I have a global.properties file and have to define the file path inside this properties file.

SheetPath=C:\\Users\\test\\Automation-Scripts\\DataTable.xlsx

This is an absolute path but require a way to define a relative path that can be consumed while calling.

2

There are 2 best solutions below

0
Nandan A On BEST ANSWER

Properties file:

testPath=API_Files/duplicateToken.json

Load the properties file:

public static Properties readProperties = new Properties();
public static void loadPropertiesFile() {
    File propertiesFile = new File(location of properties file);
    try {
        FileInputStream fileInput = new FileInputStream(propertiesFile);
        readProperties.load(fileInput);
    } catch (Exception e) {
        Logger.LogError("Error in loading the Properties file" + e.getMessage());
    }
}

Read the properties file and get absolute path:

 String testPath = readProperties.getProperty("testPath").trim();
 File absolutePath =  new File(System.getProperty("user.dir") + testPath);
 System.out.println(absolutePath);

Sample output:

C:\Users\test\Automation-Scripts\duplicateToken.json
0
Supritam On

The properties file should be relative to classpath. You can create a "configs" folder at the level of src and use the below code to read the file. Refer this for more explanation and techniques.

private Properties properties;
    private final String propertyFilePath= "configs//Configuration.properties";    
BufferedReader reader = new BufferedReader(new FileReader(propertyFilePath));
Properties properties = new Properties();
properties.load(reader);