无法从资源中读取文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法从资源中读取文件相关的知识,希望对你有一定的参考价值。
我有一个关于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文件运行项目时,如何从资源获取文件?
答案
从文件中读取资源时,与从jar中读取资源时相比,需要使用不同的咒语。如你所知,stage
任务构建了一个jar。很多人问过similar questions on StackOverflow。
我在这里重复这个答案,但是链接到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是这个问题的另一个好答案。
以上是关于无法从资源中读取文件的主要内容,如果未能解决你的问题,请参考以下文章
Apache Drill 无法从 HDFS 读取文件(资源错误:无法创建模式树)