Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A way to strip returned 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 bunch 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 an array so another function can access it?

like image 918
bleakgadfly Avatar asked Jul 06 '10 20:07

bleakgadfly


1 Answers

Those strings are just the print format for a Java File object.

See the File javadoc for which operations are available.

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

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

Or you could just use list, which returns strings in the first place:

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

If you want to read the file, you might as well keep it as a File object to pass into the core slurp or other clojure.java.io or clojure.contrib.duck-streams functions.

like image 171
j-g-faustus Avatar answered Oct 13 '22 10:10

j-g-faustus