Writing a text file

How to:

Writing text to a file using Clojure’s built-in functions

The spit function is the simplest way to write text to a file in Clojure. It takes two arguments: the file path and the string to write. If the file doesn’t exist, spit will create it. If it does, spit will overwrite it.

(spit "example.txt" "Hello, world!")

To append text to an existing file, you can use the spit function with the :append option.

(spit "example.txt" "\nLet's add this new line." :append true)

After running these snippets, “example.txt” will contain:

Hello, world!
Let's add this new line.

Using third-party libraries

While Clojure’s built-in capabilities are often sufficient, the community has developed robust libraries for more complex or specific tasks. For file I/O, one popular library is, which provides a more Java-like approach to file handling.

To use for writing to a file, you first need to import it:

(require '[ :as io])

Then, you can use the writer function to obtain a writer object, and the spit function (or others like print, println) to write to the file:

(with-open [w (io/writer "example_with_io.txt")]
  (.write w "This is written using"))

This will create (or overwrite if it already exists) “example_with_io.txt” with the text:

This is written using

Remember: with-open ensures that the file is properly closed after writing, avoiding potential resource leaks.