ไม่สามารถอ่านแฟ้มจากกองทรัพยากรได้อยู่ในโฟลเดอร์ Language

0

คำถาม

ฉันกำลังพยายามอ่านแฟ้ม"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

ตอนที่ฉัน compile มั compilation นไปด้วยดีอย่างไรก็ตามเมื่อฉันวิ่งทดสอบใน ducker ฉันกำลังตามข้อความผิดพลาดคือ:

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.

อีกพยายาม

นอกจากด้านบนฉันพยายามต่อไปนี้รหัส:

  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

แล้วก็ฉันเพิ่ม src/test/resources/data เพื่อที่ในทรัพยากร POM แฟ้ม

ในที่สุด

ที่ นี้ พูดถึงฉันตรวจสอบถ้าผมมี .txt รวมอยู่ในคอมไพเลอร์(ผมจะได้ใช้ MAC)

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

คำตอบที่ดีที่สุด

0

ฉันได้รับการแก้ปัญหาโดยการใช้ getAbsolutePath เพื่อให้การความปลอดภัยเส้นทางเดินของตัวเอง ฉันเล็งไปที่ test.properties แฟ้มที่จะมี dataDir. นี่คือสิ่งที่ฉันทำ:

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)
}

นี่ไม่ได้หมายความว่าเป็น "สุด" ทางที่จะทำอย่างนั้นแต่เผื่อว่าคุณหน้าตาที่คล้ายกันปัญหาอยู่เหนือสิ่งที่ฉันเคยที่จะต้องแก้ไขมัน

2021-11-23 17:51:30

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

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

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่