我有一个名为“silent.txt”的文件。该文件有如下一行
bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName
我想用上面的文字替换
bop4InstallDir = "/abc/xyz/pqr"
使用 groovy 脚本我该如何做到这一点?请帮忙。
我有一个名为“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 是格式良好的属性文件吗?在这种情况下,您可以使用多种方式来访问它们,比愚蠢的替换更安全。
以下代码有效:
def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
file.write(fileText);