将groovy脚本写入一个文件

将groovy脚本写入一个文件,groovy,soapui,Groovy,Soapui,我想将groovy脚本结果写入Mac计算机上的一个文件中。 在Groovy中如何实现这一点? 以下是我的尝试: log.info "Number of nodes:" + numElements log.info ("Matching codes:"+matches) log.info ("Fails:"+fails) // Exporting results to a file today = new Date() sdf = new java.text.SimpleDateFormat("

我想将groovy脚本结果写入Mac计算机上的一个文件中。 在Groovy中如何实现这一点? 以下是我的尝试:

log.info "Number of nodes:" + numElements
log.info ("Matching codes:"+matches)
log.info ("Fails:"+fails)

// Exporting results to a file
today = new Date()
sdf = new java.text.SimpleDateFormat("dd-MM-yyyy-hh-mm")
todayStr = sdf.format(today)
new File( "User/documents" + todayStr + "report.txt" ).write(numElements, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(matches, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(fails, "UTF-8" )
有人能在导出结果部分提供帮助吗

谢谢, 当做 A. 好的,我已经创建了一个

file = new File(dir + "${todayStr}_report.txt").createNewFile()
如何添加Numements、matches和fails?像这样:

 File.append (file, matches)? 
 new File("${todayStr}report.txt").write(numElements, "UTF-8")
我得到以下错误:

groovy.lang.MissingMethodException: No signature of method: static java.io.File.append() is applicable for argument types: (java.lang.Boolean, java.util.ArrayList) values: [true, [EU 4G FLAT L(CORPORATE) SP, EU 4G FLAT M(CORPORATE), ...]] Possible solutions: append(java.lang.Object, java.lang.String), append(java.io.InputStream), append(java.lang.Object), append([B), canRead(), find() error at line: 33

您有错误的文件路径。我没有MAC,所以我不是100%确定,但从我的角度来看,你应该有:

new File("/User/documents/${todayStr}report.txt").write(numElements, "UTF-8")
您至少缺少两个反斜杠,第一个在路径中的用户之前,第二个在文档之后。使用现在的方法,它尝试保存到User/documentDATE目录,非常确定它不存在

所以在上面我用绝对路径给你们指明了方向。你也可以这样写:

 File.append (file, matches)? 
 new File("${todayStr}report.txt").write(numElements, "UTF-8")
如果文件是创建的,那么您将100%确定您的文件路径有问题:)

还有几件事-由于它是groovy,请尝试利用该语言相对于Java的优势,有几种处理文件的方法,我还重写了您的日志,向您展示在groovy中处理字符串是多么简单:

log.info "Number of nodes:  ${numElements}"
log.info "Matching codes: ${matches}"
log.info "Fails: ${fails}"

我希望这有帮助

请不要再使用“不起作用”这个词;你在浪费大家的时间。相反,确切地告诉我们什么“不起作用”。您的文件路径看起来很奇怪。还要注意,
write()
每次都会覆盖文件。