如何使用clojure实例化Path对象

sak*_*zai 13 java clojure java.nio.file

由于Path类没有公共构造函数,因此使用object中的get工厂方法创建路径Paths对象.

例如

Path p2 = Paths.get("/home/admin","Migrations","/blog/tables/6-rating.xml");

//or

Path p2 = Paths.get(new URI("file://home/debianaut/Migrations/blog.sakhunzai/tables/6-rating.xml"));
Run Code Online (Sandbox Code Playgroud)

我们如何以clojure的方式做到这一点?

noi*_*ith 13

user> (java.nio.file.Paths/get "/home/justin" (into-array [".lein" "profiles.clj"]))
#<UnixPath /home/justin/.lein/profiles.clj>
Run Code Online (Sandbox Code Playgroud)

varargs java方法需要一个包含所有剩余args作为最终参数的数组.

为了使方法分派与正确的方法匹配,需要在数组外部的第一个字符串.

为了完整起见,这是一个使用URI的示例(更直接):

user> (java.nio.file.Paths/get (java.net.URI. "file:///home/justin"))
#<UnixPath /home/justin>
Run Code Online (Sandbox Code Playgroud)