clojure how to know the path of a folder / file / directory in one project?

后端 未结 3 1759
名媛妹妹
名媛妹妹 2021-02-13 11:53

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?

Ther

3条回答
  •  清歌不尽
    2021-02-13 12:24

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

提交回复
热议问题