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

As an alternative to loading the whole file into memory, you could do each line in turn

new File( 'destination.txt' ).withWriter { w ->
  new File( 'source.txt' ).eachLine { line ->
    w << line.replaceAll( 'World', 'World!!!' ) + System.getProperty("line.separator")
  }
}

Of course this (and dmahapatro’s answer) rely on the words you are replacing not spanning across lines

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)