View Category
Read the contents of a file into a string
groovy
contents = file.text
Process a file one line at a time
Open the source file to your solution and print each line in the file, prefixed by the line number, like:
1> First line of file
2> Second line of file
3> Third line of file
1> First line of file
2> Second line of file
3> Third line of file
groovy
int count = 0
file.eachLine { line ->
println "${++count} > $line"
}
file.eachLine { line ->
println "${++count} > $line"
}
file.eachLine { line, count ->
println "${++count} > $line"
}
println "${++count} > $line"
}
Write a string to a file
groovy
file.delete()
file << 'some text'
file << 'some text'
file.text = 'some text'
Append to a file
groovy
file << 'some text'
Process each file in a directory
groovy
dir.eachFile{ f -> process(f) }
Process each file in a directory recursively
groovy
dir.eachFileRecurse{ f -> process(f) }
