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. What still puzzles me is the location of the configuration file and the folder with sound clips on different operating systems. I'm Clojure noob. I know that Common Lisp has a special CL-FAD file system portability library. How is this done in closing? How to write a portable Clojure program with different file system conventions on different systems?

+7
clojure folders
source share
3 answers

You can use clojure.java.io/file to create paths in the (neutral) platform, as 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 java.io.File . If you need to return to the line, you can always use .getPath :

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

Is that what you meant?

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

+21
source share

Note that Windows perfectly supports the forward slash as a path separator (which is surprising because in this way you do not need to constantly reset the backslash).

The only significant difficulty you will encounter is that the “standard” locations (home folder, etc.) differ on Windows and UNIX systems. Therefore, you need to get them from the system properties (see the getProperty Method at http://docs.oracle.com/javase/7/docs/api/java/lang/System.html ).

+1
source share

For a platform-independent approach, you can find the canonical path from the project path, and then join it with the file name.

 (:require [clojure.java.io :as io :refer [file]])) (defn file-dir "Returns canonical path of a given path" [path] (.getCanonicalPath (io/file path))) (-> "./resources" ;; relative (file-dir) (io/file "filename.txt")) ;;=> /path/to/project/resources/filename.txt 
0
source share

All Articles