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"
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
rapt
  • 11,810
  • 35
  • 103
  • 145

2 Answers2

12

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)
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
  • 2
    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. – rapt Oct 31 '16 at 13:56
  • @rapt you did not read my answer carefully .. Use `getClass` not `classLoader` – Nagarjuna Pamu Oct 31 '16 at 13:59
  • @rapt have you tried `getClass` instead of `classLoader`. I have check it with my ide its working – Nagarjuna Pamu Oct 31 '16 at 13:59
  • Maybe you want to compare your build.sbt to mine. I had to downgrade some things there since I would get exceptions. – rapt Oct 31 '16 at 14:04
  • 1
    @rapt try again and removing this line `resourceDirectory in Compile := baseDirectory.value / "resources"` from your `build.sbt` – Nagarjuna Pamu Oct 31 '16 at 14:06
  • @rapt you dont need this line in `build.sbt` `resourceDirectory in Compile := baseDirectory.value / "resources"` – Nagarjuna Pamu Oct 31 '16 at 14:08
  • As expected removing or keeping resourceDirectory made no difference, since it's the default value. I only added it to undo Russian hackers. – rapt Oct 31 '16 at 14:13
  • i removed the "-" characters from the filename and it worked fine. did not look deeper to understand why :D – cipri.l Sep 19 '17 at 08:20
-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)

R Sun
  • 1,353
  • 14
  • 17