How to remove return values ​​from java.io.File.listFiles in Clojure

I call a java function in Clojure to get a list of files.

(require '[clojure.java.io :as io]) (str (.listFiles (io/file "/home/loluser/loldir"))) 

And I get a whole chain of strings like these

 #<File /home/loluser/loldir/lolfile1> 

etc .. How do I get rid of the brackets and put them in some form of array so that another function can access it?

+6
java file io lisp clojure
source share
1 answer

These lines are just the print format for the Java File object.

See javadoc file for which operations are available.

If you want the file paths to be strings, it would be something like

 (map #(.getPath %) (.listFiles (io/file "/home/loluser/loldir"))) 

Or you can simply use list , which returns the rows first:

 (.list (io/file "/home/loluser/loldir")) 

If you want to read a file, you can also save it as a File object to go to the core slurp or other clojure.java.io or clojure.contrib.duck-streams functions.

+7
source share

All Articles