내가 노력하는 파일 읽기"my_data.txt"앉아서 나 /src/test/resources/data
폴더에 있습니다.
––– src
–– main
–– test
––– resources
––– data
––– my_data.txt
나는 다음과 같은 코드 조각이 그렇게 하도록 요청합니다.
val filename = getClass.getResource("/src/test/resources/data/my_data.txt").getPath
때 나는 컴파일하고,컴파일이 잘 갔다,그러나,실행할 때 시험 덕에,나는 다음과 같은 오류 메시지:
java.lang.NullPointerException
at (xxxx.scala:128)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:454)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
at org.scalatest.FunSpec.withFixture(FunSpec.scala:1630)
at org.scalatest.FunSpecLike.invokeWithFixture$1(FunSpecLike.scala:452)
at org.scalatest.FunSpecLike.$anonfun$runTest$1(FunSpecLike.scala:464)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
at org.scalatest.FunSpecLike.runTest(FunSpecLike.scala:464)
at org.scalatest.FunSpecLike.runTest$(FunSpecLike.scala:446)
When I try to print the path that I am reading from I got `null` as an output.
다른 시도
위에서 떨어져 나가려고 다음과 같은 코드:
val ss = scala.io.Source.fromResource("/src/test/resources/data/my_data.txt")
ClassLoader.getSystemResource("/src/test/resources/data/my_data.txt").getPath
또한,나는 추가 src/test/resources/data
하는 리소스 POM
파일
마지막으로
로 이 언급한 선택이있는 경우 .txt
에 포함되어 있는 컴파일러(이동 MAC
)