使用groovy替换文件中的字符串 [英] Replace the string in file using groovy

查看:1225
本文介绍了使用groovy替换文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"silent.txt"的文件.该文件的内容如下所示

I have got a file with name "silent.txt". This file is having a line as follows

bop4InstallDir = myProps.cordys_install_dir + "/" + instanceName

我想用

bop4InstallDir = "/abc/xyz/pqr"

如何使用groovy脚本完成此操作? 请帮忙.

Using groovy script how do I accomplish this? Please help.

推荐答案

以下代码有效:

def file = new File("silent.txt")
def fileText = file.replaceAll("bop4InstallDir\\ \\=\\ myProps.cordys_install_dir\\ \\+\\ \"\\/\"\\ \\+\\ instanceName", "bop4InstallDir\\ \\=\\ \"/opt/cordys/bop4/defaultInst1\"")
    file.write(fileText);

这篇关于使用groovy替换文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆