Groovy adds some methods to the URL
class which make reading data from an URL easy. For example the text
property returns the complete contents of the document that is identified by the URL. Or we can use the eachLine()
method to loop through each line of the document. Groovy also adds a toURL()
to the String
class to make a URL
object.
// Contents of http://www.mrhaki.com/url.html: // Simple test document // for testing URL extensions // in Groovy. def url = "http://www.mrhaki.com/url.html".toURL() assert '''\ Simple test document for testing URL extensions in Groovy. ''' == url.text def result = [] url.eachLine { if (it =~ /Groovy/) { result << it } } assert ['in Groovy.'] == result url.withReader { reader -> assert 'Simple test document' == reader.readLine() }