tags:

views:

36

answers:

3

Hi I want to read whole file contents in groovy on command line. I am using this code

#!/usr/bin/env groovy


   def xmlData =""

 System.in.withReader {

        xmlData=xmlData+ it.readLine()

} 

println xmlData

and the following command

cat 1.xml | /root/a.groovy

But it only reads one line from the file. I want to read whole file.

Any suggestion?? Regards Shuja

+1  A: 
it.eachLine{ line -> ... }

If you don't need to bufferize the whole file in memory just avoid to read it entirely, it will be resource demanding.. I can assure you from tests I had to do :)

Jack
+2  A: 

To read a line at a time:

System.in.eachLine { line ->
    xmlData += line
}

Or to read the whole thing in one go:

xmlData = System.in.text
ataylor
A: 

Try

#!/usr/bin/env groovy
def xmlData = System.in.text
println xmlData

instead.

binil