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.
CodePudding user response:
Properties file:
testPath=API_Files/duplicateToken.json
Load the properties file:
public static void loadPropertiesFile() {
File propertiesFile = new File(location of properties file);
try {
FileInputStream fileInput = new FileInputStream(propertiesFile);
propertiesFile.load(fileInput);
} catch (Exception e) {
Logger.LogError("Error in loading the Properties file" e.getMessage());
}
}
Read the properties file and get absolute path:
public static Properties readProperties = new Properties();
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
CodePudding user response:
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);