how to replace a string/word in a text file in groovy

前端 未结 6 2225
难免孤独
难免孤独 2020-12-05 22:57

Hello I am using groovy 2.1.5 and I have to write a code which show the contens/files of a directory with a given path then it makes a backup of the file and replace a word/

6条回答
  •  执念已碎
    2020-12-05 23:39

    I use this code to replace port 8080 to ${port.http} directly in certain file:

        def file = new File('deploy/tomcat/conf/server.xml')
        def newConfig = file.text.replace('8080', '${port.http}')
        file.text = newConfig
    

    The first string reads a line of the file into variable. The second string performs a replace. The third string writes a variable into file.

提交回复
热议问题