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?
6 Answers
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')

- 185,044
- 174
- 569
- 824
-
1Do I have to execute some close() statements or will the reader be closed by the getText() method? – das Keks Apr 29 '14 at 08:27
-
6@dasKeks I think it's safe to assume that the implementation of this method closes any necessary resources. Anyway, you don't have access to any reader that may be created, so you *can't* close it – Dónal Apr 29 '14 at 11:26
-
I'd like to mention that this works even if the `File` object originated from an ordinary java jar. I wasn't sure if maybe Groovy had its own special `File` class with the `text` attribute, or something, but it seems that it doesn't matter where the `File` object comes from, whether it's instantiated by Groovy code or Java code. – ArtOfWarfare Mar 06 '17 at 20:44
-
FWIW, I found that unless I dropped `String ` from the beginning of that line, the variable ended up empty. – roens Apr 10 '18 at 22:00
-
2@roens That makes no sense. I suspect there is some other factor in the mix like you had a local variable that was hiding a field or something like that. – Jeff Scott Brown Apr 26 '18 at 20:17
The shortest way is indeed just
String fileContents = new File('/path/to/file').text
but in this case you have no control on how the bytes in the file are interpreted as characters. AFAIK groovy tries to guess the encoding here by looking at the file content.
If you want a specific character encoding you can specify a charset name with
String fileContents = new File('/path/to/file').getText('UTF-8')
See API docs on File.getText(String)
for further reference.
-
10+1 for recommending the version that takes an encoding parameter. The plain `someFile.text` doesn't make an intelligent guess, it simply uses the platform default encoding (usually UTF-8 on modern Linuxes, but something like windows-1252 or MacRoman on Windows/Mac OS, unless you've overridden it with `-Dfile.encoding=...`) – Ian Roberts Aug 07 '13 at 15:47
-
In recent versions of Groovy at least, the guess is done using [CharsetToolkit](https://docs.groovy-lang.org/latest/html/api/groovy/util/CharsetToolkit.html) which does make an intelligent guess. – Matthijs Bierman May 11 '20 at 12:47
A slight variation...
new File('/path/to/file').eachLine { line ->
println line
}
-
5
-
It's a good thing to see this--makes processing a file line by line trivial. – Bill K Nov 19 '18 at 17:46
In my case new File()
doesn't work, it causes a FileNotFoundException
when run in a Jenkins pipeline job. The following code solved this, and is even easier in my opinion:
def fileContents = readFile "path/to/file"
I still don't understand this difference completely, but maybe it'll help anyone else with the same trouble. Possibly the exception was caused because new File()
creates a file on the system which executes the groovy code, which was a different system than the one that contains the file I wanted to read.

- 1,593
- 15
- 27
-
1Does readFile step works fine for you? for me it works but it makes ' ' single quotes on some words, I don't understand ! – sirineBEJI Jun 14 '18 at 15:07
-
It works fine for me, and didn't encounter any quotes I didn't place myself (I guess you mean quotes in the file contents). Maybe worth to create a new question for this? In that case, try to specify in which cases the quotes appear and where exactly. – P Kuijpers Jun 19 '18 at 08:21
-
4Works for jenkins. as readFile is an internal keyword, and does not need any import or extra approval from jenkins-admin. The whole file can be read in String var and then printed via below code: `String fp_f = readFile("any_file") if (fp.length()) { currentBuild.description = fp }` Also, if file is not found then there is error. – ashish Jun 19 '18 at 09:07
-
5Btw. the reason for this is, that new File() looks for files on your computer, as readFile in Jenkins looks in the groovy sandbox of the java vm where your pipeline stuff is probably running ... Also you are allowed to use readfile in sandboxed pipelines but using File() is by default not allowed, File you will have to whitelist in Jenkins settings before you can use it. – Cyborg-X1 Feb 16 '20 at 11:56
the easiest way would be
which means you could just do:
new File(filename).text

- 22,894
- 45
- 188
- 319

- 39,701
- 6
- 59
- 77
Here you can Find some other way to do the same.
Read file.
File file1 = new File("C:\Build\myfolder\myTestfile.txt");
def String yourData = file1.readLines();
Read Full file.
File file1 = new File("C:\Build\myfolder\myfile.txt");
def String yourData= file1.getText();
Read file Line Bye Line.
File file1 = new File("C:\Build\myfolder\myTestfile.txt");
for (def i=0;i<=30;i++) // specify how many line need to read eg.. 30
{
log.info file1.readLines().get(i)
}
Create a new file.
new File("C:\Temp\FileName.txt").createNewFile();

- 104
- 2
- 5
-
1
-
Without using **def** code will work, I have a habit to define those variable! – shashi singh Oct 31 '18 at 08:59
-
I like readLines() approach, because it works as in a normal (console) groovy as in the Jenkins Pipeline. – Alexander Samoylov Apr 13 '21 at 12:13