Clojure how to find the path to a folder / file / directory in one project?

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

Exists:

(System/getProperty "user.dir") 

or

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

But this gives where the current directory is. The one that includes the clj file, the one in which the code is written. But how to find out the path to the txt file? The goal is to write to this txt file from the clj file.

Thanks.

+8
functional-programming clojure
source share
3 answers

In Java and therefore Clojure you can find files on CLASSPATH. For example, in Java, it is customary to place things like log4j.properties at the top of your CLASSPATH (for example, in the class directory), and then you can link to the file in Clojure (or Java) code with:

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

Do you use and run your application with Leiningen? If so, you can create a directory at the top level and put the files there. For example, if you have a configuration file, you might have a "conf" dir with properties files:

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

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

 (slurp "conf/myproj.conf") 
+7
source share

The Clojure local-file library allows you to get the current project directory with local-file/project-dir . As long as you know where the file you want to access is located in your project, you should find it this way.

+3
source share

This gives where the current clj file is the one that this code is written.

No, it is not. It provides the current directory.

Do you consider that you can run clojure scripts that are not in the current directory?

+1
source share

All Articles