score:-1

I'm able to get the output by putting dummy.txt in src/test/resources and running a test case.

println(Source.fromResource("dummy.txt").getLines.length)

score:11

Use getClass not classLoader and put / in front of the resource name /war-and-peace.txt. war-and-peace.txt file must be in resources folder (src/main/resources).

val file = getClass.getResource("/war-and-peace.txt").getFile()

Source.fromFile(file).getLines.foreach(println)

in one line

Source.fromFile(getClass.getResource("/war-and-peace.txt").getFile).getLines().foreach(println)

getClass.getResourceAsStream is more reliable as it works when the code is packaged inside a jar file also

Source.fromInputStream(getClass.getResourceAsStream("/war-and-peace.txt")).getLines().foreach(println)

Related Query

More Query from same tag