gpt4 book ai didi

java - 如何使用grails中的脚本覆盖所有打印方法的功能

转载 作者:行者123 更新时间:2023-12-02 15:48:27 24 4
gpt4 key购买 nike

在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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com