我想将 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()
如何添加元素数量、匹配和失败?像这样:
File.append (file, matches)?
我收到以下错误:
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,很确定它不存在。
上面我向您展示了绝对路径的方法。你也可以严格这样写:
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}"
希望对你有帮助
关于groovy - 将 Groovy 脚本结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163688/