Depending on what you want to do with File, there may be a shorter path. Note that the URL has GDK methods getText() , eachLine{}etc.
Figure 1:
def file = new File(getClass().getResource('/resource.xml').toURI())
def list1 = []
file.eachLine { list1 << it }
def list2 = []
getClass().getResource('/resource.xml').eachLine {
list2 << it
}
assert list1 == list2
Figure 2:
import groovy.xml.*
def xmlSlurper = new XmlSlurper()
def url = getClass().getResource('/resource.xml')
def file = new File(url.toURI())
def root1 = xmlSlurper.parseText(file.text)
def root2 = xmlSlurper.parseText(url.text)
assert root1.text() == root2.text()
source
share