首页 > 解决方案 > 如何将 groovyconsole 的输出保存在文件中?

问题描述

我已经在 AEM 中安装了 groovyconsole 并且有一堆我想保存在文件中作为输出的东西。

这篇文章之后,我尝试了:

def filePath = "/content/corporate/reports/output.csv"
File output = new File(filePath)

output.append('Hello world!')

我最终得到一个 FileNotFoundException。我直接创建文件只是为了确保,但这也无济于事。

希望对此有所了解。谢谢。

标签: groovyaemgroovy-console

解决方案


我建议您检查以下内容:

  • 您可能没有写入指定文件夹的权限。您应该首先尝试写入您绝对具有写入权限的文件夹。
  • 可能并非所有导致 output.csv 的目录都存在,您应该使用如下方式确保文件夹树存在File.mkdirs()

def folderPath = "~/fff/eee/reports"
def folder = new File(folderPath)
def result =folder.mkdirs()

def filePath = "~/fff/eee/reports/output.csv"
File output = new File(filePath)

output.withWriterAppend{ out ->
    out.println 'hello there!'
}

推荐阅读