Nie można odczytać pliku z folderu zasobów w Scala

0

Pytanie

Próbuję odczytać pliku "my_data.txt" to siedzi w moim /src/test/resources/data katalog.

––– src

  –– main

  –– test

   ––– resources

      ––– data

          ––– my_data.txt

Do tego mam następujący fragment kodu:

val filename = getClass.getResource("/src/test/resources/data/my_data.txt").getPath

Kiedy go компилирую, kompilacja się udała, jednak gdy uruchamiam test w ducker, otrzymuję następujący komunikat o błędzie:

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.

Jeszcze jedna próba

Oprócz powyższych, próbowałem następujący kod:

  1. val ss = scala.io.Source.fromResource("/src/test/resources/data/my_data.txt")
  2. ClassLoader.getSystemResource("/src/test/resources/data/my_data.txt").getPath

Ponadto, dodałem src/test/resources/data do zasobów w POM plik

Ostatecznie

Jak już wspomniano, sprawdziłem, czy mam.txt all inclusive w kompilator ( używam MAC)

intellij-idea pom.xml resources
2021-11-21 16:34:50
1

Najlepsza odpowiedź

0

Cóż, postanowiłem ten problem z pomocą getAbsolutePath aby uzyskać ścieżkę bezwzględną. Zwróciłem uwagę na test.properties plik, aby mieć dataDir. Oto co zrobiłem:

properties.load(getClass.getClassLoader.getResourceAsStream("test.properties"))
testDataDirectory = new File(properties.getProperty("dataDir"))
val file = new File(testDataDirectory.getAbsolutePath +"/data/my_data.txt"

for (line <- Source.fromFile(file).getLines) {
   println(line)
}

To nie oznacza, że jest to "najlepszy" sposób, aby to zrobić, ale na wszelki wypadek, jeśli można spotkać się z podobnym problemem, powyżej tego, że użyłem do jej rozwiązania.

2021-11-23 17:51:30

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................