为什么以下代码无法从文件夹
我的项目中的文件夹结构是由Intellij创建的标准Scala结构。
因为某些原因,只有当我将文件放在
我正在使用。
resources
中读取 war-and-peace.txt
文件?我的项目中的文件夹结构是由Intellij创建的标准Scala结构。
因为某些原因,只有当我将文件放在
src/main/scala
目录下(即我的Scala代码所在的位置),它才会读取此文件,但是当我将它放在 src/main/resources
目录下时,它会忽略此文件(在后一种情况下,我得到了一个 java.lang.NullPointerException
异常,顺便提一下,如果您打算建议这样做,那么当我尝试读取 “/war-and-peace.txt”
(带有前导斜杠)时,我也会遇到相同的异常)。val file = new File(classLoader.getResource("war-and-peace.txt").getFile())
Source.fromFile(file).....
我正在使用。
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"
classLoader
,而是使用getClass
(getClass.getResource("/war-and-peace.txt").getFile()
)。 - Nagarjuna Pamu