How to read a file in Groovy into a string?

前端 未结 6 1363
不知归路
不知归路 2020-12-02 05:12

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条回答
  •  悲&欢浪女
    2020-12-02 06:10

    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.

提交回复
热议问题