如何在全局中定义文件路径.属性文件吗?



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

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

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

属性文件:

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:UserstestAutomation-ScriptsduplicateToken.json

属性文件应该相对于类路径。您可以创建一个"config "SRC级别的文件夹,并使用下面的代码读取该文件。要了解更多的解释和技术,请参考本文。

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

最新更新