Dark theme | Light theme

August 9, 2010

Groovy Goodness: Simple File Renaming

Groovy has a lot of simple useful methods to make every day life easier. In Groovy 1.7.4 we can use the renameTo() method on a File object. We pass a String argument with the new name of the file.

def file = new File('test.groovy')
file.text = 'simple content'
file.renameTo 'newname.groovy'

def newFile = new File('newname.groovy')
assert newFile.exists()
assert 'simple content' == newFile.text