Clojure - File I/O



Clojure provides a number of helper methods when working with I/O. It offers easier classes to provide the following functionalities for files.

  • Reading files
  • Writing to files
  • Seeing whether a file is a file or directory

Let’s explore some of the file operations Clojure has to offer.

Reading the Contents of a File as an Entire String

If you want to get the entire contents of the file as a string, you can use the clojure.core.slurp method. The slurp command opens a reader on a file and reads all its contents, returning a string.

Following is an example of how this can be done.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def string1 (slurp "Example.txt"))
   (println string1))
(Example)

If the file contains the following lines, they will be printed as −

line : Example1
line : Example2

Reading the Contents of a File One Line at a Time

If you want to get the entire contents of the file as a string one line at a time, you can use the clojure.java.io/reader method. The clojure.java.io/reader class creates a reader buffer, which is used to read each line of the file.

Following is an example that shows how this can be done.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (with-open [rdr (clojure.java.io/reader "Example.txt")]
   (reduce conj [] (line-seq rdr))))
(Example)

If the file contains the following lines, they will be printed as −

line : Example1
line : Example2

The output will be shown as −

["line : Example1" "line : Example2"]

Writing ‘to’ Files

If you want to write ‘to’ files, you can use the clojure.core.spit command to spew entire strings into files. The spit command is the opposite of the slurp method. This method opens a file as a writer, writes content, then closes file.

Following is an example.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (spit "Example.txt"
      "This is a string"))

In the above example, if you see the contents of the Example.txt file , you will see the contents of “This is a string”.

Writing ‘to’ Files One Line at a Time

If you want to write ‘to’ files one line at a time, you can use the clojure.java.io.writer class. The clojure.java.io.writer class is used to create a writer stream wherein bytes of data are fed into the stream and subsequently into the file.

Following is an example that shows how the spit command can be used.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (with-open [w (clojure.java.io/writer "Example.txt" :append true)]
      (.write w (str "hello" "world"))))
(Example)

When the above code is executed, the line “hello world” will be present in the Example.txt file. The append:true option is to append data to the file. If this option is not specified, then the file will be overwritten whenever data is written to the file.

Checking to See If a File Exists

To check if a file exists, the clojure.java.io.file class can be used to check for the existence of a file. Following is an example that shows how this can be accomplished.

(ns clojure.examples.hello
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (println (.exists (clojure.java.io/file "Example.txt"))))
(Example)

If the file Example.txt exists, the output will be true.

Reading from the Console

To read data from the console, the read-line statement can be used. Following is an example that shows how this can be used.

If you enter the (read-line) command in the REPL window, you will have the chance to enter some input in the console window.

user->(read-line)
Hello World

The above code will produce the following output.

“Hello World”
Advertisements