Make Clojure recognize and isolate the line in the file

I am trying to get Clojure to read a file, put the first line in a variable, and the rest in another variable. I can’t figure out how to do this, and I would like someone to give me a head,

+5
source share
2 answers
;; for Clojure 1.1
(require '[clojure.contrib.duck-streams :as io])
;; for bleeding edge
(require '[clojure.java.io :as io])

(with-open [fr (io/reader "/path/to/the/file")]
  (let [[first-line & other-lines] (doall (line-seq fr))]
    ;; do stuff with the lines below...
    ...))

Update: Ah, I just realized that I took "the rest" from the question to mean "the rest of the lines in the file", so in the above other-linesthere are seq of all lines in the file except the first.

", ", , (require '[clojure.contrib.str-utils2 :as str])/(require '[clojure.string :as str]) ( Clojure) (str/join "\n" other-lines), other-lines ; , , - :

(let [contents (slurp "/path/to/the/file")
      [first-line rest-of-file] (.split #"\n" contents 2)]
  ...)
+4

Clojure head:

(require '[clojure.string :as str])
(let [[f & r] (str/split (slurp "foo.txt") #"\n")]
   ... do something with f and r ...)

ED: - Michał , clojure.string.split, .

+2

All Articles