java - 如何在 global.properties 文件中定义文件路径?
问题描述
我有一个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:\Users\test\Automation-Scripts\duplicateToken.json
推荐阅读
- c# - 无法在 C# ConcurrentDictionary 上调用 PropertyChanged 事件
- google-apps-script - Google Script 正在输出图像文件路径而不是图像
- react-native - 向苹果钱包添加票时如何点击“添加”?
- c# - 在 C# 中捕获由 3rd-Party 库生成的调试输出
- rust - 如何为 GTK 条目完成编写自定义匹配函数?
- javascript - 当我使用 JS 隐藏/取消隐藏时,Flask 不返回数据库查询。如果我注释掉 JS 就可以了
- c# - 如何在 c# 中使用 text.box 中的变量更新 oracle 数据库表
- r - 在任何给定日期计算观察值
- c# - 实体框架 - 有条件地查询具有类型转换的相关实体
- google-tag-manager - Google Ads 再营销代码不起作用 - 仅在预览和调试模式下