我有一个关于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 run启动项目时 - 没关系,文件存在
[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文件运行项目时,如何从资源获取文件?
答案 0 :(得分:2)
从文件读取资源时,与从jar中读取资源时相比,需要使用不同的咒语。如您所知,stage
任务构建了一个jar。很多人问过similar questions on StackOverflow。
我在这里重复@iny的答案,但是链接到Java 8运行时库,因为Scala目前支持它:
要进行汇总,您应该使用java.lang.Class.getResourceAsStream(String)
,请参阅https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-
This是这个问题的另一个好答案。