【发布时间】:2014-03-07 08:59:40
【问题描述】:
我有一个名为“silent.txt”的文件。该文件有如下一行
bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName
我想把上面的文字替换成
bop4InstallDir = "/abc/xyz/pqr"
使用 groovy 脚本如何完成此操作? 请帮忙。
【问题讨论】:
标签: grails groovy groovyshell
我有一个名为“silent.txt”的文件。该文件有如下一行
bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName
我想把上面的文字替换成
bop4InstallDir = "/abc/xyz/pqr"
使用 groovy 脚本如何完成此操作? 请帮忙。
【问题讨论】:
标签: grails groovy groovyshell
以下代码有效:
def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
file.write(fileText);
【讨论】:
silent.txt 是格式良好的属性文件吗?在这种情况下,您可以使用多种方式来访问它们,比愚蠢的替换更安全。
【讨论】:
不是很优雅,但这应该可以。
def file = new File("silent.txt")
file.text = file.text.replace('bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName',
'bop4InstallDir = "/abc/xyz/pqr"')
【讨论】: