2017-04-20 39 views
0

在grails中,我们可以在脚本中重定向打印方法并使用GroovyShell运行,如下所示。如何在grails中使用脚本覆盖所有打印方法的功能

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 etc.Please帮助我。

回答

0

您可以尝试设置jvm的输出流。

System.out = new PrintStream(new FileOutputStream(...)) 

我看到您使用的是script.out,这可能也适用。

这应该为您正在使用的任何System.print方法设置默认输出流。