Clojure Guides_ Working with Files and Directories in Clojure
Clojure Guides_ Working with Files and Directories in Clojure
This cookbook covers working with files and directories from Clojure, using functions in the
clojure.java.io namespace as well as parts of the JDK via interoperability.
This work is licensed under a Creative Commons Attribution 3.0 Unported License
(https://creativecommons.org/licenses/by/3.0/) (including images & stylesheets). The source is available
on Github (https://github.com/clojure-doc/clojure-doc.github.io).
Preliminaries
Note that for the examples below, "io" is an alias for clojure.java.io . That is, it's assumed your ns
macro contains:
Recipes
Read a file into one long string
(def a-long-string (slurp "foo.txt"))
Note, you can pass urls to slurp as well. See also slurp at Clojuredocs
(https://clojuredocs.org/clojure.core/slurp).
Note: mapv is eager and returns a vector. If you use map here, the reader will be closed before the whole
sequence is realized so you would need to wrap the call to map in a call to doall anyway. The lines that
line-seq gives you have no trailing newlines (and empty lines in the file will yield empty strings ( "" )).
Write a long string out to a new file
(spit "foo.txt"
"A long
multi-line string.
Bye.")
Is it a directory? :
An io/file is a java.io.File object (a file or a directory). You can call a number of functions on it,
including:
See also
https://github.com/clj-commons/fs (https://github.com/clj-commons/fs)
the I/O section of the cheatsheet (https://clojure.org/api/cheatsheet)
Links
About (/articles/about/)
Table of Contents (/articles/content/)
Getting Started (/articles/tutorials/getting_started/)
Introduction to Clojure (/articles/tutorials/introduction/)
Clojure Editors (/articles/tutorials/editors/)
Clojure Community (/articles/ecosystem/community/)
Basic Web Development (/articles/tutorials/basic_web_development/)
Language: Functions (/articles/language/functions/)
Language: clojure.core (/articles/language/core_overview/)
Language: Collections and Sequences (/articles/language/collections_and_sequences/)
Language: Namespaces (/articles/language/namespaces/)
Language: Java Interop (/articles/language/interop/)
Language: Polymorphism (/articles/language/polymorphism/)
Language: Concurrency and Parallelism (/articles/language/concurrency_and_parallelism/)
Language: Macros (/articles/language/macros/)
Language: Laziness (/articles/language/laziness/)
Language: Glossary (/articles/language/glossary/)
Ecosystem: Library Development and Distribution (/articles/ecosystem/libraries_authoring/)
Ecosystem: Web Development (/articles/ecosystem/web_development/)
Ecosystem: Generating Documentation (/articles/ecosystem/generating_documentation/)
Building Projects: tools.build and the Clojure CLI (/articles/cookbooks/cli_build_projects/)
Data Structures (/articles/cookbooks/data_structures/)
Strings (/articles/cookbooks/strings/)
Mathematics with Clojure (/articles/cookbooks/math/)
Date and Time (/articles/cookbooks/date_and_time/)
Working with Files and Directories in Clojure
Middleware in Clojure (/articles/cookbooks/middleware/)
Parsing XML in Clojure (/articles/cookbooks/parsing_xml_with_zippers/)
Growing a DSL with Clojure (/articles/cookbooks/growing_a_dsl_with_clojure/)