clojure如何知道一个项目中的文件夹/文件/目录的路径?(clojure how to know the path of a folder / file / directory in one pr

编程入门 行业动态 更新时间:2024-10-12 16:22:50
clojure如何知道一个项目中的文件夹/文件/目录的路径?(clojure how to know the path of a folder / file / directory in one project?)

假设我的clojure项目中有两个文件,一个是clj,另一个是txt。 有没有办法从clj文件中知道txt文件的路径(作为字符串)?

有:

(System/getProperty "user.dir")

要么

(-> (java.io.File. ".") .getAbsolutePath)

但是这给了当前目录。 包含clj文件的代码,其中包含代码。但如何知道txt文件的路径? 目的是从clj文件写入这个txt文件。

谢谢。

Assume there is two files inside my clojure project, one clj and the other is txt. Is there a way to know the path (as a string) of the txt file from the clj file?

There is:

(System/getProperty "user.dir")

or

(-> (java.io.File. ".") .getAbsolutePath)

But this gives where the current directory. The one that includes the clj file, the one the code is written in. But how to know the path of the txt file? The purpose is to write into this txt file from the clj file.

Thank you.

最满意答案

在Java中,因此Clojure可以在CLASSPATH上找到文件。 例如,在Java中,通常将诸如log4j.properties之类的东西放在CLASSPATH的顶部(例如,在classes目录中),然后可以使用以下方法在Clojure(或Java)代码中引用该文件:

(java.io.File. "log4j.properties")

您是否正在使用Leiningen并运行您的应用程序? 如果是这样,您可以在顶层创建一个目录并将文件放在那里。 例如,如果你有一个配置文件,你可以有一个带有属性文件的“conf”目录:

my-lein-proj$ ls conf doc project.clj README.md src target test

假设你将一个myproj.conf文件放在conf目录中,并且你想在你的Clojure代码中读取它。 然后你可以做:

(slurp "conf/myproj.conf")

In Java and therefore Clojure you can find files on the CLASSPATH. For example, in Java it is common to put things like log4j.properties at the top of your CLASSPATH (e.g., in the classes directory) and then you can reference the file in your Clojure (or Java) code with:

(java.io.File. "log4j.properties")

Are you using and running your app with Leiningen? If so, you can create a directory at the top level and put files there. For example, if you have a config file you can have a "conf" dir with a properties files:

my-lein-proj$ ls conf doc project.clj README.md src target test

Suppose you put a myproj.conf file in the conf directory and you want to read from it in your Clojure code. Then you can just do:

(slurp "conf/myproj.conf")

更多推荐

本文发布于:2023-08-07 14:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464532.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   路径   文件   目录   项目

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!