简体   繁体   English

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

[英]Why doesn't clojure.java.io/resource work in this case?

I'm trying to load an absolute resource via clojure.java.io/resource and it cannot locate it. 我正在尝试通过clojure.java.io/resource加载绝对资源,但找不到它。 However, I can retrieve it by other means. 但是,我可以通过其他方式检索它。

The example below demonstrates this. 下面的示例演示了这一点。 Create a file in the base src directory for a fresh project, and attempts to locate it via. 在基本src目录中为一个新项目创建一个文件,然后尝试通过它定位。 resource do not succeed, but I can get to it via. resource不会成功,但是我可以通过它获得成功。 .getResource on the System class. System类上的.getResource

How can I load this via. 我如何通过加载。 resource ? resource Any ideas why this is like this? 任何想法为什么会这样?

eg 例如

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=>

You need to add src to your resource-paths in project.clj: 您需要将src添加到project.clj中的资源路径中:

:resource-paths ["src"]

Those directories are included on the classpath and thus available to load resources. 这些目录包含在类路径中,因此可用于加载资源。

Also, you need to remove the leading / : 另外,您需要删除前导/

(io/resource "world.txt")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Clojure:clojure.java.io/resource如何加载文件? - Clojure: How does clojure.java.io/resource load file? clojure.java.io ClassNotFoundException - clojure.java.io ClassNotFoundException 要编写下载器,请使用Clojure.java.io还是Java的io api? - To write a downloader, Clojure.java.io or Java's io api? 我如何使用 lein exec 要求 clojure.java.io? - How do I require clojure.java.io with lein exec? 没有方法的实现::协议的as-file:#&#39;clojure.java.io /为类发现的强制:clojure.lang.PersistentVector - No implementation of method: :as-file of protocol: #'clojure.java.io/Coercions found for class: clojure.lang.PersistentVector 没有实现方法:: make-reader of protocol:#&#39;clojure.java.io / IOFactory for class:nil - No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil clojure的读取文件结构,即with-open和clojure.java.io/reader,是否足够频繁访问? - Is clojure's read file structure, i.e. with-open and clojure.java.io/reader, efficient enough for frequent access? Clojure Clostache错误-没有实现方法::make-reader of protocol:#&#39;clojure.java.io / IOFactory为类nil找到 - Clojure Clostache error - No implementation of method: :make-reader of protocol: #'clojure.java.io/IOFactory found for class: nil 为什么printStackTrace不能在Clojure中运行? - Why doesn't printStackTrace work in Clojure? Clojure,实施范围,为什么此解决方案不起作用 - Clojure, implement range, why this solution doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM