How to read a file in Groovy into a string

filegroovy

I need to read a file from the file system and load the entire contents into a string in a groovy controller, what's the easiest way to do that?

Best Answer

String fileContents = new File('/path/to/file').text

If you need to specify the character encoding, use the following instead:

String fileContents = new File('/path/to/file').getText('UTF-8')