0% found this document useful (0 votes)
27 views

Clojure Guides_ Working with Files and Directories in Clojure

Uploaded by

eowug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Clojure Guides_ Working with Files and Directories in Clojure

Uploaded by

eowug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

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

or else in the repl you've loaded it:

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

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).

Read a file one line at a time


Suppose you'd like to call my-func on every line in a file, and return the resulting sequence:

(with-open [rdr (io/reader "foo.txt")]


(mapv my-func (line-seq rdr)))

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.")

Overwrites the file if it already exists. To append, use

(spit "foo.txt" "file content" :append true)

Write a file one line at a time


Although you could do this by calling spit with :append true for each line, that would be inefficient
(opening the file for appending for each call). Instead, use with-open to keep a file open while writing
each line to it:

(with-open [wrtr (io/writer "foo.txt")]


(doseq [i my-vec]
(.write wrtr (str i "\n"))))

Check if a file exists


(.exists (io/file "filename.txt"))

Is it a directory? :

(.isDirectory (io/file "path/to/something"))

An io/file is a java.io.File object (a file or a directory). You can call a number of functions on it,
including:

exists Does the file exist?


isDirectory Is the File object a directory?
getName The basename of the file.
getParent The dirname of the file.
getPath Filename with directory.
mkdir Create this directory on disk.

To read about more available methods, see the java.io.File docs


(https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html).

Get a list of the files and dirs in a given directory


As File objects:
(.listFiles (io/file "path/to/some-dir"))

Same, but just the names (strings), not File objects:

(.list (io/file "path/to/some-dir"))

The results of those calls are seqable.

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)

« Date and Time (/articles/cookbooks/date_and_time/) || Middleware in Clojure »


(/articles/cookbooks/middleware/)

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/)

Copyright © 2024 Multiple Authors


Powered by Cryogen (https://cryogenweb.org)

You might also like