liveBook · Manning

Character encoding

As explained in Clojure: The Essential Reference

A character encoding is a scheme that maps characters to byte sequences and decodes byte sequences into characters.

Character encoding maps characters to byte sequences and decodes byte sequences back into characters. In Clojure I/O, an encoding controls how readers interpret input and writers produce output. section 22.8

Use the :encoding option when the resource uses something other than the default UTF-8. The chosen encoding must match the file’s actual encoding; specifying UTF-16 for another format is incorrect. section 22.8

Reading and writing UTF-16

(with-open [r (io/reader "/usr/share/dict/words" :encoding "UTF-16") w (io/writer "/tmp/words" :append true :encoding "UTF-16")] (doseq [line (line-seq r)] (.write w (str (upper-case line) "\n")))) ;; Processes lines lazily and appends UTF-16 output. section 22.8

(with-open [r (reader "/usr/share/dict/words" :encoding "UTF-16")
            w (writer "/tmp/words" :append true :encoding "UTF-16")] ; #1
  (doseq [line (line-seq r)]
    (.write w (str (upper-case line) "\n")))) ; #2

Browse the complete UTF-16 reader-and-writer example, including append behavior and line processing. section 22.8

How Clojure: The Essential Reference covers it

How deep it goes
Defines encoding selection and matching requirements but stops short of byte-level implementation details.
Examples
Demonstrates uppercasing dictionary lines while reading and appending UTF-16 output.
What it assumes
Assumes familiarity with Clojure readers and writers while teaching where to select an encoding.
What you can do after
Operate/configure Clojure file processing with an explicitly selected character encoding.
How it connects
Connects readers, writers, streams, files, and lazy line processing.

Also explained in

Compare how 4 books explain character encoding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.