clojure如何知道一个文件夹/文件/目录在一个项目的路径? [英] clojure how to know the path of a folder / file / directory in one project?

查看:90
本文介绍了clojure如何知道一个文件夹/文件/目录在一个项目的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的clojure项目中有两个文件,一个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?

有:

(System/getProperty "user.dir") 

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

但这给出了当前目录的位置。包括clj文件,代码是写在一个。
但是如何知道txt文件的路径?
目的是从clj文件写入这个txt文件。

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.

谢谢。

推荐答案

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

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")

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

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

一个myproj.conf文件在conf目录,你想从它在你的Clojure代码中读取。那么你可以这样做:

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")

这篇关于clojure如何知道一个文件夹/文件/目录在一个项目的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆