首页 > 解决方案 > 如何在 global.properties 文件中定义文件路径?

问题描述

我有一个global.properties文件,必须在此属性文件中定义文件路径。

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

这是一个绝对路径,但需要一种方法来定义可以在调用时使用的相对路径。

标签: javaseleniumselenium-webdriverrest-assuredrelative-url

解决方案


属性文件:

testPath=API_Files/duplicateToken.json

加载属性文件:

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());
    }
}

读取属性文件并获取绝对路径:

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

样本输出:

C:\Users\test\Automation-Scripts\duplicateToken.json

推荐阅读