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

前端 未结 6 2222
难免孤独
难免孤独 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:44

    Answers that use "File" objects are good and quick, but usually cause following error that of course can be avoided but at the cost of loosen security:

    Scripts not permitted to use new java.io.File java.lang.String. Administrators can decide whether to approve or reject this signature.

    This solution avoids all problems presented above:

    String filenew = readFile('dir/myfile.yml').replaceAll('xxx','YYY')
    writeFile file:'dir/myfile2.yml', text: filenew
    

提交回复
热议问题