gpt4 book ai didi

java - 将时间戳变量添加到应用程序属性中的文件夹路径值

转载 作者:行者123 更新时间:2023-12-01 18:00:12 24 4
gpt4 key购买 nike

所以我需要在 app.properties 文件中设置一个文件夹路径名称值。我还想以当前时间戳命名它,这样当它用于创建文件时,它也会创建文件夹。我目前所拥有的不起作用。

screenshot.events = STARTED,SUCCEEDED,FAILED,STEP
screenshot.path = C:/Automation/${timestamp}
webdriver.type=CHROME

最佳答案

这里有 3 个选项:

1。启动时

您可以在启动 Spring Boot 应用程序时定义所需的 SystemProperty:

public static void main(String[] args) {
System.setProperty("timestamp",String.valueOf(System.currentTimeMillis()));

new SpringApplicationBuilder() //
.sources(Launcher.class)//
.run(args);
}

然后,按照您的方式在 application.properties 中定义您的属性:

screenshot.path = C:/Automation/${timestamp}

2。注入(inject)时

@Value("${screenshot.path}")
public void setScreenshotPath(String screenshotPath) {
this.screenshotPath =
screenshotPath.replace("${timestamp}", System.currentTimeMillis());
}

3。使用时 - 执行时的动态时间戳

@Value("${screenshot.path}")
private String screenshotPath;
...
new File(screenshotPath.replace("${timestamp}", System.currentTimeMillis());

//or the following without the need for ${timestamp} in screenshot.path
//new File(screenshotPath + System.currentTimeMillis());

关于java - 将时间戳变量添加到应用程序属性中的文件夹路径值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41597748/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com