Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails 使用groovy替换文件中的字符串_Grails_Groovy_Groovyshell - Fatal编程技术网

Grails 使用groovy替换文件中的字符串

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

我有一个名为“silent.txt”的文件。此文件有一行,如下所示

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脚本。