Scala does not read file from resources folder
Asked Answered
T

2

5

Why does the following not read the war-and-peace.txt file from the resources folder?

The folder structure in my project is the standard Scala structure that is created be Intellij.

For some reason it only reads this file when I put it in the src/main/scala (i.e. where my Scala code is), but ignores this file when I put it in the src/main/resources (in the latter case I am getting java.lang.NullPointerException, BTW I am getting the same exception also when I try to read "/war-and-peace.txt" (with a preceding slash) in case you were thinking to suggest it).

val file = new File(classLoader.getResource("war-and-peace.txt").getFile())

Source.fromFile(file).....

I am using

java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

build.sbt:

name := "my-project"

version := "1.0"

scalaVersion := "2.11.8"

resourceDirectory in Compile := baseDirectory.value / "resources"

libraryDependencies += "junit" % "junit" % "4.12"

libraryDependencies += "org.scalatest" % "scalatest_2.11" % "2.2.4"
Truda answered 31/10, 2016 at 13:12 Comment(1)
do not use classLoader use getClass (getClass.getResource("/war-and-peace.txt").getFile())Whack
W
13

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)
Whack answered 31/10, 2016 at 13:15 Comment(8)
I have tried similar suggestions that had been published in SO in the past. But I am still getting NullPointerException once I remove the input file from the scala folder and keep it only in the resources folder.Truda
@Truda you did not read my answer carefully .. Use getClass not classLoaderWhack
@Truda have you tried getClass instead of classLoader. I have check it with my ide its workingWhack
Maybe you want to compare your build.sbt to mine. I had to downgrade some things there since I would get exceptions.Truda
@Truda try again and removing this line resourceDirectory in Compile := baseDirectory.value / "resources" from your build.sbtWhack
@Truda you dont need this line in build.sbt resourceDirectory in Compile := baseDirectory.value / "resources"Whack
As expected removing or keeping resourceDirectory made no difference, since it's the default value. I only added it to undo Russian hackers.Truda
i removed the "-" characters from the filename and it worked fine. did not look deeper to understand why :DIsoelectronic
P
-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)

Pedaiah answered 11/1, 2020 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.