14 December 2013

How to display number of lines in file in Groovy

This is code to display number of lines in file in Groovy
CODE:

package dms.pastor.lab.groovy

class Stats {
    static main(args) {
        def num = 0;
        new File("C:\\Dom\\workspaces\\sts\\lab\\LabGroovy\\src\\dms\\pastor\\lab\\groovy\\Stats.groovy").eachLine { num++ }
        println "File has " + num + " lines.";
    }
}

based partly on solution from:
http://robbyoconnor.blogspot.co.uk/2008/04/groovy-way-to-count-number-of-lines.html