为什么clojure.java.io/resource在这种情况下不起作用?

时间:2014-04-08 14:51:58

标签: clojure

我正在尝试通过clojure.java.io/resource加载绝对资源,但无法找到它。但是,我可以通过其他方式检索它。

以下示例说明了这一点。在基本src目录中为新项目创建一个文件,并尝试通过它找到它。 resource没有成功,但我可以通过它来实现。 System类上的.getResource

如何加载此通道。 resource?任何想法为什么会这样?

e.g。

C:\Users\username\temp>lein new foo
Generating a project called foo based on the 'default' template.
To see other templates (app, lein plugin, etc), try `lein help new`.

C:\Users\username\temp>cd foo   
C:\Users\username\temp\foo>cd src    
C:\Users\username\temp\foo\src>echo hello > world.txt   
C:\Users\username\temp\foo\src>cd ..    
C:\Users\username\temp\foo>lein repl

...    

user=> (require ['clojure.java.io :as 'io])
nil
user=> (io/resource "/world.txt")
nil
user=> (.getResource System "/world.txt")
#<URL file:/C:/Users/username/temp/foo/src/world.txt>
user=>

1 个答案:

答案 0 :(得分:5)

您需要将src添加到project.clj中的资源路径:

:resource-paths ["src"]

这些目录包含在类路径中,因此可用于加载资源。

此外,您需要删除前导/

(io/resource "world.txt")