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

4

18

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.

Wellinformed answered 24/2, 2013 at 9:30 Comment(0)
B
9

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")
Blessing answered 24/2, 2013 at 14:13 Comment(1)
Hi, I am not using Leiningen, just clojure project. the need is just for a path as a string to a txt file in the project directory. (the use of this path is to write to this txt file from the clj file).Thank you.Wellinformed
D
3

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

Descriptive answered 24/2, 2013 at 20:51 Comment(1)
Hi, may you demonstrate? the need is just for a path as a string to a txt file in the project directory. (the use of this path is to write to this txt file from the clj file).Thank you.Wellinformed
C
2

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

No, it doesn't. It gives the current directory.

Did you take into account that one can run clojure scripts that are not in the current directory?

Compliancy answered 24/2, 2013 at 10:6 Comment(2)
Hi, yes you are right, but how to know the path to an empty txt file in the project folder or to create a new txt file inside the project folder. In either case, what is needed is the path to this txt file as a string. And all need to be a code in clojure file.Wellinformed
If you don't need a project but only a clojure file, install the latest Clojure 1.9 that has clj utility on board and follow the instruction: clojure.org/reference/deps_and_cliRooster
R
0

if your file structure is something similar to this:

config | src | target | test

you have a config inside config directory, in our case let's assume its java.config and you are trying to read this file inside core.clj in src directory you can also use clojure.java.io/reader method

for example:

(clojure.java.io/reader "config/java.config")

you can run below commands in the repl to see the contents of the file

(slurp (clojure.java.io/reader "config/java.config")

if you are interested in reading the contents of the file line by line you combine above function with with-open method and read line by line:

(with-open [word (clojure.java.io/reader "config/java.config")]
  (loop [c (.read word)]
    (if (not= c -1)
      (do
        (print (char c))
        (recur (.read word))))))
Rowles answered 30/4, 2021 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.