无法从资源读取文件



我在Scala和SBT上有一个项目。我尝试从资源获取文件。

  val filename = getClass.getResource("/emptyClickReports.csv").getFile
  log.debug("get empty report {} from resource folder {} ", filePath, filename)
  val file = new File(filename)
  log.debug("file exists: {}", file.exists())
  log.debug("file getAbsolutePath: {}", file.getAbsolutePath())
  log.debug("file getCanonicalPath: {}", file.getCanonicalPath())
  log.debug("file getPath: {}", file.getPath())
  file

当我使用SBT运行启动项目时 - 可以,文件存在

[DEBUG] - 2017-12-07 14:25:09,469 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - get empty report /emptyClickReports.csv from resource folder /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv 
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file exists: true
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getAbsolutePath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getCanonicalPath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:25:09,470 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getPath: /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv
[INFO ] - 2017-12-07 14:25:09,471 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.scenarios.controllers.ScenarioController - file downloaded /home/slava/projects/ds-selenium/target/scala-2.11/classes/emptyClickReports.csv, file length: 97

但是当我进行SBT阶段并使用bin文件运行时,我将有接下来:

[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file exists: false
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getAbsolutePath: /home/slava/projects/ds-selenium/file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getCanonicalPath: /home/slava/projects/ds-selenium/file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[DEBUG] - 2017-12-07 14:58:50,085 - ds-selenium - 9d0ca610-0291-4ae0-8fd9-18229ca0641e - c.dsources.selenium.common.services.FileManipulationsService - file getPath: file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv
[

文件不退出,路径为:

file:/home/slava/projects/ds-selenium/target/universal/stage/lib/ds-selenium.ds-selenium-0.1.5.jar!/emptyClickReports.csv

使用bin文件运行项目时,如何从资源中获取文件?

从文件中读取资源时,您需要与从jar中读取资源时不同的咒语。如您所知,stage任务构建了一个JAR。许多人在Stackoverflow上问了类似的问题。

我在此处通过@INY重复该答案,但是使用Java 8运行时库的链接,因为Scala当前支持:

要摘要,您应该使用java.lang.Class.getResourceAsStream(String),请参见https://docs.oracle.com/javase/8/docs/api/java/java/lang/lang/class.html#getresourceasourceasourceasourceasream-java-java.lang.lang.string-string-

这是这个问题的另一个好答案。

相关内容

  • 没有找到相关文章