Manipulating files and directories
In this recipe, we will show you how to read from files and write to files. Then we will learn how to create, delete, and list files and directories.
Getting ready
This recipe does not make use of any external library, so you can just start a REPL and be ready. As we manipulate files and directories, be careful to not delete files or directories and not overwriting the contents of your important files through inattention. We will make use of the source code of Clojure on the GitHub, so you need to install the git
client to obtain the source.
How to do it...
Let's learn how to read and write files. We will also learn how to create and delete files and directories. Then we will see how to list files and directories.
spit and slurp
The spit
writes a string to a file and the slurp
reads the contents of a file as string:
(spit "/tmp/hello.txt" "Hello World Nico!" ) ;;=> nil
The spit
with the :append true
option appends a string to an existing file:
(spit...