Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a Specific Line of a File with Groovy

Tags:

groovy

Can some one help me to read a file line by line,

I have this code - but this code will print all the content. I need to display only the 5th(or specific) line by line, hence I want to access and print any line dynamically. I need to print 5th line of the text file.

//read from file

myFile = new File("C:\\Documents and Settings\\ABCEDFG\\Desktop\\soapUI\\params.txt")
printFileLine = { log.info "File line: " + it }
myFile.eachLine(0, printFileLine)

Please help -Appreciate your help in advance!

^Thanks

like image 972
ABCDEFG Avatar asked Nov 22 '11 07:11

ABCDEFG


1 Answers

It's sloppy and wasteful, but you can do

log.info "Line 5: " +  myFile.readLines().get(4)
like image 68
Art Taylor Avatar answered Nov 15 '22 11:11

Art Taylor