Skip to content

Instantly share code, notes, and snippets.

@saidone75
Created March 4, 2020 16:30
Show Gist options
  • Save saidone75/0844f40d5f2d8b129cb7302b7cf40541 to your computer and use it in GitHub Desktop.
Save saidone75/0844f40d5f2d8b129cb7302b7cf40541 to your computer and use it in GitHub Desktop.
Get file creation time in Clojure using java.nio
(defn creation-time [file]
(.creationTime
(java.nio.file.Files/readAttributes
(.toPath file)
java.nio.file.attribute.BasicFileAttributes
(into-array java.nio.file.LinkOption []))))
@simon-brooke
Copy link

Generalised as:

(:import [java.nio.file Files FileSystems LinkOption])

(defn file-attribute
  "Return the value of the specified `attribute` of the file at `file-path`
   in the current default file system. The argument `attribute` may be passed
   as a keyword or a string, but must be an attribute name understood be 
   `java.nio.file.Files`."
  [file-path attribute]
  (Files/getAttribute
   (.getPath
    (FileSystems/getDefault)
    (str file-path)
    (into-array java.lang.String []))
   (name attribute)
   (into-array LinkOption [])))

@saidone75
Copy link
Author

Thank you very much, I will use your solution from now on!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment