Groovy write to file (newline)

前端 未结 5 1529
你的背包
你的背包 2020-12-24 01:05

I created a small function that simply writes text to a file, but I am having issues making it write each piece of information to a new line. Can someone explain why it puts

5条回答
  •  北海茫月
    2020-12-24 01:43

    As @Steven points out, a better way would be:

    public void writeToFile(def directory, def fileName, def extension, def infoList) {
      new File("$directory/$fileName$extension").withWriter { out ->
        infoList.each {
          out.println it
        }
      }
    }
    

    As this handles the line separator for you, and handles closing the writer as well

    (and doesn't open and close the file each time you write a line, which could be slow in your original version)

提交回复
热议问题