Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure file-system portability

I want to write a simple program for playing sound clips. I want to deploy it on Windows, Linux and MacOSX. The thing that still puzzles me is location of configuration file and folder with sound clips on different operating systems. I am a Clojure noob. I am aware that Common Lisp has special file-system portability library called CL-FAD. How it is being done in Closure? How can I write portable Clojure program with different file system conventions on different systems?

like image 900
ruby_object Avatar asked Jan 18 '14 00:01

ruby_object


1 Answers

You can use clojure.java.io/file to build paths in a (mostly) platform-neutral way, similarly to how you would with os.path.join in Python or File.join in Ruby.

(require '[clojure.java.io :as io])

;; On Linux
(def home "/home/jbm")
(io/file home "media" "music") ;=> #<File /home/jbm/media/music>

;; On Windows
(def home "c:\\home\\jbm")
(io/file home "media" "music") ;=> #<File c:\home\jbm\media\music>

clojure.java.io/file returns a java.io.File. If you need to get back to a string you can always use .getPath:

(-> home
  (io/file "media" "music")
  (.getPath))
;=> /home/jbm/media/music"

Is that the sort of thing you had in mind?

In addition to clojure.java.io (and, of course, the methods on java.io.File), raynes.fs is a popular file system utility library.

like image 120
jbm Avatar answered Oct 05 '22 21:10

jbm