Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to neatly print new line in Groovy?

Tags:

groovy

I was wondering if there is any neat way to write new line into the file from Groovy. I have the following script:

new File("out.txt").withWriter{ writer ->
    for(line in 0..100) {
            writer << "$line"
    }
}

I could use writer << "$line\n" or writer.println("$line"), but I was wondring if there is any way to use << operator to append the new line for me.

like image 206
Tomasz Błachowicz Avatar asked Sep 15 '10 13:09

Tomasz Błachowicz


1 Answers

It's a nice idea to ask the system for the correct line separator as these can change between operating systems.

writer << "Line 1" + System.getProperty("line.separator") + "Line 2"
like image 188
Treethawat Thanawachiramate Avatar answered Dec 06 '22 12:12

Treethawat Thanawachiramate