How to read a file in Groovy into a string?

前端 未结 6 1365
不知归路
不知归路 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:09

    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();
    

提交回复
热议问题