Clojure was designed to cover the Java platform, and this is one area where Clojure does not provide its own API. This means that you probably have to dive into Java, but the classes you need to work with are perfectly used directly from Clojure.
One class that you should read in javadocs is java.io.File , which represents the file path.
http://docs.oracle.com/javase/6/docs/api/java/io/File.html
The instance method .listFiles returns an array (which you can use as seq) of File objects β one for each entry in the directory identified by the File instance. There are other useful methods for determining whether a File exists, whether a directory, etc.
Example
(ns my-file-utils (:import java.io.File)) (defn my-ls [d] (println "Files in " (.getName d)) (doseq [f (.listFiles d)] (if (.isDirectory f) (print "d ") (print "- ")) (println (.getName f)))) ;; Usage: (my-ls (File. "."))
Building File Objects
The File constructor can sometimes be a little inconvenient to use (especially when merging many path segments at a time), in which case Clojure provides a useful helper function: clojure.java.io/file . As arguments, it takes path segments as strings or files. Segments are connected to the correct platform path separator.
http://clojuredocs.org/clojure_core/clojure.java.io/file
Note: Clojure also provides a file-seq function that returns a preliminary move (like seq), although the file hierarchy begins with the given file.
raek Dec 19 '11 at 20:37 2011-12-19 20:37
source share