在grails中,我们可以重定向脚本中的打印方法,并使用GroovyShell
运行,如下所示。
def scriptText = '''
def s = "Groovy rocks!"
println s
printf 'The answer is %X', 42
'''
def shell = new GroovyShell()
def script = shell.parse(scriptText)
def stringWriter = new StringWriter()
script.out = new PrintWriter(stringWriter)
script.run()
assert stringWriter.toString() == 'Groovy rocks!\nThe answer is 2A'
但我想将打印输出到文件中并执行println,printf或println等的默认功能。请帮助我。
最佳答案
您可以尝试设置jvm的输出流。
System.out = new PrintStream(new FileOutputStream(...))
我看到您正在使用
script.out
,它也可能起作用。这应该为您使用的任何System.print方法设置默认输出流。
关于java - 如何使用grails中的脚本覆盖所有打印方法的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43516112/