Saturday, April 3, 2010

File Handling in Groovy

File Handling in Groovy

File handling in Groovy is made significantly easier with the addition of variousmethods to classes in the java.io package. A great example is the File.eachLine method.

FileHandle.gy

def number=0new File ('Nik.txt').eachLine {
line -> number++
println "$number: $line"
}

Nik.txt

My name is Ayush.
But you can call me Nik.
I am fully devoted to Java and want to something extra for java

Now run groovy file

D:\Nik\Groovy_Example>groovy FileHandle.gy
1: My name is Ayush.
2: But you can call me Nik.
3: I am fully devoted to Java and want to something extra for java

D:\Nik\Groovy_Example>

No comments:

Post a Comment