Home > front end >  Groovy script doesn't find file in resources
Groovy script doesn't find file in resources

Time:12-04

I have a small Gradle project I just started that has a single Groovy script in src/main/groovy and a text file in src/main/resources/input/myInput.txt. My script just has this content currently:

def food = [:]
currentFood = 0
currentElf = 0

new File('src/main/resources/input/myInput.txt').eachLine { line ->
    if (line.isBlank()) {
        food[currentElf  ] = currentFood
        currentFood = 0
    } else {
        currentFood  = line.toInteger()
    }
}

However, when I run it, I get java.io.FileNotFoundException: src/main/resources/input/myInput.txt. This is pretty much straight from this Baeldung article, which are usually pretty reliable. What is going wrong here?

CodePudding user response:

Instead of loading it via File, load it via the class and getResourcesAsStream like so:

this.getClass().getResourceAsStream("/input/day-1-small.txt").eachLine { line ->
  • Related