Clojure core environment and runtime library providing a dynamic, general-purpose programming language on the JVM.
—
File system operations, stream processing, and I/O utilities through clojure.java.io and core I/O functions. Clojure provides comprehensive I/O capabilities for working with files, streams, and resources.
Basic file system operations and file handling.
(slurp f & opts)
;; Function: reads entire file into string
;; f can be file path, File, URL, URI, etc.
;; opts: :encoding charset (default platform encoding)
;; Returns: String
(spit f content & opts)
;; Function: writes content to file
;; content can be string or anything writable
;; opts: :encoding charset, :append boolean
;; Returns: nil
(clojure.java.io/file path & more)
;; Function: creates File object from path components
;; Returns: java.io.File
(clojure.java.io/delete-file f & [silently])
;; Function: deletes file, optionally ignoring errors
;; Returns: boolean (true if successful)
(clojure.java.io/make-parents f)
;; Function: creates parent directories for file
;; Returns: boolean (true if successful)Creating readers, writers, and streams for I/O operations.
(clojure.java.io/reader x & opts)
;; Function: creates BufferedReader from various sources
;; x can be file path, File, URL, URI, InputStream, etc.
;; opts: :encoding charset, :buffer-size int
;; Returns: java.io.BufferedReader
(clojure.java.io/writer x & opts)
;; Function: creates BufferedWriter to various destinations
;; x can be file path, File, URI, OutputStream, etc.
;; opts: :encoding charset, :buffer-size int, :append boolean
;; Returns: java.io.BufferedWriter
(clojure.java.io/input-stream x & opts)
;; Function: creates InputStream from various sources
;; Returns: java.io.InputStream
(clojure.java.io/output-stream x & opts)
;; Function: creates OutputStream to various destinations
;; opts: :append boolean
;; Returns: java.io.OutputStreamLoading resources from classpath and URLs.
(clojure.java.io/resource name)
;; Function: finds resource on classpath
;; Returns: java.net.URL or nil
(clojure.java.io/resource name loader)
;; Function: finds resource using specific ClassLoader
;; Returns: java.net.URL or nil
(clojure.java.io/as-file x)
;; Function: coerces to File if possible
;; Returns: java.io.File or nil
(clojure.java.io/as-url x)
;; Function: coerces to URL if possible
;; Returns: java.net.URL
(clojure.java.io/as-relative-path x)
;; Function: returns relative path string
;; Returns: StringReading and writing data streams.
(clojure.java.io/copy input output & opts)
;; Function: copies data from input to output
;; input/output can be various stream types
;; opts: :buffer-size int, :encoding charset
;; Returns: nil
(line-seq rdr)
;; Function: returns lazy sequence of lines from BufferedReader
;; Returns: clojure.lang.LazySeq
(with-open [bindings*] & body)
;; Macro: ensures streams are closed after use
;; bindings: [name stream-expr name stream-expr ...]
;; Returns: result of last body expressionCore Clojure functions for input and output operations.
(print & more)
;; Function: prints objects to *out* using print-str
;; Returns: nil
(println & more)
;; Function: prints objects to *out* followed by newline
;; Returns: nil
(printf fmt & args)
;; Function: prints formatted string to *out*
;; Uses Java String.format formatting
;; Returns: nil
(pr & more)
;; Function: prints objects to *out* in readable form
;; Returns: nil
(prn & more)
;; Function: prints objects to *out* in readable form with newline
;; Returns: nil
(print-str & xs)
;; Function: returns string representation of objects
;; Returns: String
(println-str & xs)
;; Function: returns string representation with newline
;; Returns: String
(pr-str & xs)
;; Function: returns readable string representation
;; Returns: String
(prn-str & xs)
;; Function: returns readable string representation with newline
;; Returns: StringFunctions for reading input from various sources.
(read)
(read stream)
(read stream eof-error-p eof-value)
;; Function: reads one object from *in* or stream
;; Returns: Object
(read-line)
;; Function: reads line from *in*
;; Returns: String or nil
(read-string s)
;; Function: reads one object from string
;; Returns: Object
(load-file name)
;; Function: loads and evaluates Clojure source file
;; Returns: result of last expression
(load-reader rdr)
;; Function: loads and evaluates Clojure code from reader
;; Returns: result of last expression
(load-string s)
;; Function: loads and evaluates Clojure code from string
;; Returns: result of last expressionSpecial variables that control I/O behavior.
*in*
;; Dynamic var: current input stream (default System/in)
;; Type: java.io.Reader
*out*
;; Dynamic var: current output stream (default System/out)
;; Type: java.io.Writer
*err*
;; Dynamic var: current error stream (default System/err)
;; Type: java.io.Writer
*print-length*
;; Dynamic var: maximum items to print in collections (nil = unlimited)
;; Type: Long or nil
*print-level*
;; Dynamic var: maximum nesting depth to print (nil = unlimited)
;; Type: Long or nil
*print-meta*
;; Dynamic var: whether to print metadata (default false)
;; Type: boolean
*print-readably*
;; Dynamic var: whether output should be readable (default true)
;; Type: booleanAdditional file system operations and utilities.
;; File properties (using Java File methods)
(.exists file) ; File exists?
(.isFile file) ; Is regular file?
(.isDirectory file) ; Is directory?
(.canRead file) ; Can read file?
(.canWrite file) ; Can write file?
(.length file) ; File size in bytes
(.lastModified file) ; Last modified timestamp
(.getParent file) ; Parent directory path
(.getName file) ; File name
(.getAbsolutePath file) ; Absolute path
(.listFiles file) ; List directory contents
;; Creating directories
(.mkdir file) ; Create single directory
(.mkdirs file) ; Create directory treeUsage Examples:
;; File reading and writing
(spit "test.txt" "Hello, World!")
(slurp "test.txt") ; => "Hello, World!"
;; Appending to files
(spit "log.txt" "Log entry 1\n" :append true)
(spit "log.txt" "Log entry 2\n" :append true)
;; Working with readers and writers
(with-open [rdr (clojure.java.io/reader "data.txt")]
(doall (line-seq rdr)))
(with-open [wtr (clojure.java.io/writer "output.txt")]
(.write wtr "Line 1\n")
(.write wtr "Line 2\n"))
;; Resource loading
(clojure.java.io/resource "config.properties")
(slurp (clojure.java.io/resource "data.json"))
;; File operations
(def f (clojure.java.io/file "path" "to" "file.txt"))
(.exists f) ; => false
(clojure.java.io/make-parents f) ; Create parent dirs
(.getAbsolutePath f) ; => "/current/path/to/file.txt"
;; Copying data
(with-open [in (clojure.java.io/input-stream "source.txt")
out (clojure.java.io/output-stream "dest.txt")]
(clojure.java.io/copy in out))
;; Reading from URL
(with-open [rdr (clojure.java.io/reader "https://example.com/data.json")]
(slurp rdr))
;; Processing large files line by line
(with-open [rdr (clojure.java.io/reader "large-file.txt")]
(doseq [line (line-seq rdr)]
(process-line line)))
;; Print functions
(println "Hello" "World") ; Prints: Hello World
(printf "Name: %s, Age: %d\n" "Alice" 30) ; Prints: Name: Alice, Age: 30
(pr {:name "Alice" :age 30}) ; Prints: {:name "Alice", :age 30}
;; Reading input
(print "Enter your name: ")
(flush)
(def name (read-line))
;; Dynamic binding for output redirection
(with-open [wtr (clojure.java.io/writer "output.log")]
(binding [*out* wtr]
(println "This goes to the file")
(prn {:data "structure"})))
;; Controlling print behavior
(binding [*print-length* 3]
(pr (range 10))) ; Prints: (0 1 2 ...)Install with Tessl CLI
npx tessl i tessl/maven-org-clojure--clojure