Grails 使用groovy替换文件中的字符串
我有一个名为“silent.txt”的文件。此文件有一行,如下所示Grails 使用groovy替换文件中的字符串,grails,groovy,groovyshell,Grails,Groovy,Groovyshell,我有一个名为“silent.txt”的文件。此文件有一行,如下所示 bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName 我想将上面的文本替换为 bop4InstallDir = "/abc/xyz/pqr" 使用groovy脚本如何实现这一点? 请帮忙。不是很优雅,但这应该行得通 def file = new File("silent.txt") file.text = file.text.replace('bo
bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName
我想将上面的文本替换为
bop4InstallDir = "/abc/xyz/pqr"
使用groovy脚本如何实现这一点?
请帮忙。不是很优雅,但这应该行得通
def file = new File("silent.txt")
file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName',
'bop4InstallDir = "/abc/xyz/pqr"')
silent.txt的属性文件格式是否正确?在这种情况下,您可以使用各种方式访问它们,比使用dumb replace更安全 看
或以下代码起作用:
def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
file.write(fileText);
它不起作用。该错误类似于“expecting EOF,found’”@第6行第34列。“bop4InstallDir=“/abc/xyz/pqr”)“我认为您将引号放错了位置。请按上面的方式复制代码,然后重试。复制的代码无效。我需要处理一些特殊字符,比如“/”、“+”……它不是一个属性文件。这是一个groovy脚本。