gpt4 book ai didi

java - Groovy - 如何创建内部包含变量的路径目录

转载 作者:行者123 更新时间:2023-12-02 06:14:06 26 4
gpt4 key购买 nike

我有一个 groovy 脚本来创建一个包含结果的文本文件

我定义的路径文件是 C:/temp/SoapTest/并且我想动态创建一个新文件夹,该文件夹在任何新用户测试中都会更改,因此我想使用定义的变量作为文件夹,因此本例中的变量是 Affected CI 。

这是我的完整代码:

nl=System.getProperty("line.separator");
def Date0 = testRunner.testCase.testSuite.getPropertyValue( "Date")
def DateIn = testRunner.testCase.testSuite.getPropertyValue( "DateIn")
def Summary = testRunner.testCase.testSuite.getPropertyValue( "Summary")
def SummaryIn = testRunner.testCase.testSuite.getPropertyValue( "SummaryIn")
def RandomID = testRunner.testCase.testSuite.getPropertyValue( "RandomID")
def AffectedCI = testRunner.testCase.testSuite.getPropertyValue( "AffectedCI")
def SourceStatus = testRunner.testCase.testSuite.getPropertyValue( "SourceStatus")
def event = 'Active-Inactive'
def INC = testRunner.testCase.testSuite.getPropertyValue( "INC")
def IID_Active = testRunner.testCase.testSuite.getPropertyValue( "IID Active")
def IID_Inactive = testRunner.testCase.testSuite.getPropertyValue( "IID Inactive")
class LeftShiftNewlineCategory {
static Writer leftShift(Writer self, Object value) {
self.append value + "\n\r"
}
}
use(LeftShiftNewlineCategory) {
def pathfile = 'C:/temp/SoapTest/'
def subpath = 'Active-Inactive '
def fileformat = '.txt'
def FinRes = AffectedCI
def datetime = new Date().format(" dd MM yy' 'HH-mm-ss")
def file = new File(pathfile + subpath + FinRes + datetime + fileformat).withWriter { out ->
out << "Date:" +"\t"+"\t"+"\t"+ Date0
out << "${nl}DateIn:" +"\t"+"\t"+"\t"+ DateIn
out << "${nl}RandomID:" +"\t"+"\t"+ RandomID
out << "${nl}AffectedCI:" +"\t"+"\t"+ AffectedCI
out << "${nl}SourceStatus" +"\t"+"\t"+ SourceStatus
out << "${nl}event" +"\t"+"\t"+"\t"+ event
out << "${nl}INC" +"\t"+"\t"+"\t"+ INC
out << "${nl}IID Active" +"\t"+"\t"+ IID_Active
out << "${nl}IID Inactive" +"\t"+"\t"+ IID_Inactive
out << "${nl}"
out << "${nl}Payload:" +"\t"+"\t"+ Summary
out << "${nl}"
out << "${nl}PayloadIn:" +"\t"+"\t"+ SummaryIn
}
}

提前非常感谢您!

最佳答案

可能您需要先创建父目录:

def file = new File(pathfile + subpath + FinRes + datetime + fileformat)
def success = file.parentFile.mkdirs()
println "created ${file.parentFile}: ${success}"
file.withWriter { out ->
...
}

关于java - Groovy - 如何创建内部包含变量的路径目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55882688/

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