or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

concurrency.mdcontrol-flow.mdcore-language.mddata-structures.mddata-utilities.mddev-tools.mdformats.mdindex.mdio.mdjava-interop.mdmath.mdsequences.mdstrings.mdtesting.md

io.mddocs/

0

# I/O Operations

1

2

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.

3

4

## Capabilities

5

6

### File Operations

7

8

Basic file system operations and file handling.

9

10

```clojure { .api }

11

(slurp f & opts)

12

;; Function: reads entire file into string

13

;; f can be file path, File, URL, URI, etc.

14

;; opts: :encoding charset (default platform encoding)

15

;; Returns: String

16

17

(spit f content & opts)

18

;; Function: writes content to file

19

;; content can be string or anything writable

20

;; opts: :encoding charset, :append boolean

21

;; Returns: nil

22

23

(clojure.java.io/file path & more)

24

;; Function: creates File object from path components

25

;; Returns: java.io.File

26

27

(clojure.java.io/delete-file f & [silently])

28

;; Function: deletes file, optionally ignoring errors

29

;; Returns: boolean (true if successful)

30

31

(clojure.java.io/make-parents f)

32

;; Function: creates parent directories for file

33

;; Returns: boolean (true if successful)

34

```

35

36

### Stream Creation

37

38

Creating readers, writers, and streams for I/O operations.

39

40

```clojure { .api }

41

(clojure.java.io/reader x & opts)

42

;; Function: creates BufferedReader from various sources

43

;; x can be file path, File, URL, URI, InputStream, etc.

44

;; opts: :encoding charset, :buffer-size int

45

;; Returns: java.io.BufferedReader

46

47

(clojure.java.io/writer x & opts)

48

;; Function: creates BufferedWriter to various destinations

49

;; x can be file path, File, URI, OutputStream, etc.

50

;; opts: :encoding charset, :buffer-size int, :append boolean

51

;; Returns: java.io.BufferedWriter

52

53

(clojure.java.io/input-stream x & opts)

54

;; Function: creates InputStream from various sources

55

;; Returns: java.io.InputStream

56

57

(clojure.java.io/output-stream x & opts)

58

;; Function: creates OutputStream to various destinations

59

;; opts: :append boolean

60

;; Returns: java.io.OutputStream

61

```

62

63

### Resource Loading

64

65

Loading resources from classpath and URLs.

66

67

```clojure { .api }

68

(clojure.java.io/resource name)

69

;; Function: finds resource on classpath

70

;; Returns: java.net.URL or nil

71

72

(clojure.java.io/resource name loader)

73

;; Function: finds resource using specific ClassLoader

74

;; Returns: java.net.URL or nil

75

76

(clojure.java.io/as-file x)

77

;; Function: coerces to File if possible

78

;; Returns: java.io.File or nil

79

80

(clojure.java.io/as-url x)

81

;; Function: coerces to URL if possible

82

;; Returns: java.net.URL

83

84

(clojure.java.io/as-relative-path x)

85

;; Function: returns relative path string

86

;; Returns: String

87

```

88

89

### Stream Processing

90

91

Reading and writing data streams.

92

93

```clojure { .api }

94

(clojure.java.io/copy input output & opts)

95

;; Function: copies data from input to output

96

;; input/output can be various stream types

97

;; opts: :buffer-size int, :encoding charset

98

;; Returns: nil

99

100

(line-seq rdr)

101

;; Function: returns lazy sequence of lines from BufferedReader

102

;; Returns: clojure.lang.LazySeq

103

104

(with-open [bindings*] & body)

105

;; Macro: ensures streams are closed after use

106

;; bindings: [name stream-expr name stream-expr ...]

107

;; Returns: result of last body expression

108

```

109

110

### Core I/O Functions

111

112

Core Clojure functions for input and output operations.

113

114

```clojure { .api }

115

(print & more)

116

;; Function: prints objects to *out* using print-str

117

;; Returns: nil

118

119

(println & more)

120

;; Function: prints objects to *out* followed by newline

121

;; Returns: nil

122

123

(printf fmt & args)

124

;; Function: prints formatted string to *out*

125

;; Uses Java String.format formatting

126

;; Returns: nil

127

128

(pr & more)

129

;; Function: prints objects to *out* in readable form

130

;; Returns: nil

131

132

(prn & more)

133

;; Function: prints objects to *out* in readable form with newline

134

;; Returns: nil

135

136

(print-str & xs)

137

;; Function: returns string representation of objects

138

;; Returns: String

139

140

(println-str & xs)

141

;; Function: returns string representation with newline

142

;; Returns: String

143

144

(pr-str & xs)

145

;; Function: returns readable string representation

146

;; Returns: String

147

148

(prn-str & xs)

149

;; Function: returns readable string representation with newline

150

;; Returns: String

151

```

152

153

### Input Functions

154

155

Functions for reading input from various sources.

156

157

```clojure { .api }

158

(read)

159

(read stream)

160

(read stream eof-error-p eof-value)

161

;; Function: reads one object from *in* or stream

162

;; Returns: Object

163

164

(read-line)

165

;; Function: reads line from *in*

166

;; Returns: String or nil

167

168

(read-string s)

169

;; Function: reads one object from string

170

;; Returns: Object

171

172

(load-file name)

173

;; Function: loads and evaluates Clojure source file

174

;; Returns: result of last expression

175

176

(load-reader rdr)

177

;; Function: loads and evaluates Clojure code from reader

178

;; Returns: result of last expression

179

180

(load-string s)

181

;; Function: loads and evaluates Clojure code from string

182

;; Returns: result of last expression

183

```

184

185

### Dynamic I/O Variables

186

187

Special variables that control I/O behavior.

188

189

```clojure { .api }

190

*in*

191

;; Dynamic var: current input stream (default System/in)

192

;; Type: java.io.Reader

193

194

*out*

195

;; Dynamic var: current output stream (default System/out)

196

;; Type: java.io.Writer

197

198

*err*

199

;; Dynamic var: current error stream (default System/err)

200

;; Type: java.io.Writer

201

202

*print-length*

203

;; Dynamic var: maximum items to print in collections (nil = unlimited)

204

;; Type: Long or nil

205

206

*print-level*

207

;; Dynamic var: maximum nesting depth to print (nil = unlimited)

208

;; Type: Long or nil

209

210

*print-meta*

211

;; Dynamic var: whether to print metadata (default false)

212

;; Type: boolean

213

214

*print-readably*

215

;; Dynamic var: whether output should be readable (default true)

216

;; Type: boolean

217

```

218

219

### File System Utilities

220

221

Additional file system operations and utilities.

222

223

```clojure { .api }

224

;; File properties (using Java File methods)

225

(.exists file) ; File exists?

226

(.isFile file) ; Is regular file?

227

(.isDirectory file) ; Is directory?

228

(.canRead file) ; Can read file?

229

(.canWrite file) ; Can write file?

230

(.length file) ; File size in bytes

231

(.lastModified file) ; Last modified timestamp

232

(.getParent file) ; Parent directory path

233

(.getName file) ; File name

234

(.getAbsolutePath file) ; Absolute path

235

(.listFiles file) ; List directory contents

236

237

;; Creating directories

238

(.mkdir file) ; Create single directory

239

(.mkdirs file) ; Create directory tree

240

```

241

242

**Usage Examples:**

243

244

```clojure

245

;; File reading and writing

246

(spit "test.txt" "Hello, World!")

247

(slurp "test.txt") ; => "Hello, World!"

248

249

;; Appending to files

250

(spit "log.txt" "Log entry 1\n" :append true)

251

(spit "log.txt" "Log entry 2\n" :append true)

252

253

;; Working with readers and writers

254

(with-open [rdr (clojure.java.io/reader "data.txt")]

255

(doall (line-seq rdr)))

256

257

(with-open [wtr (clojure.java.io/writer "output.txt")]

258

(.write wtr "Line 1\n")

259

(.write wtr "Line 2\n"))

260

261

;; Resource loading

262

(clojure.java.io/resource "config.properties")

263

(slurp (clojure.java.io/resource "data.json"))

264

265

;; File operations

266

(def f (clojure.java.io/file "path" "to" "file.txt"))

267

(.exists f) ; => false

268

(clojure.java.io/make-parents f) ; Create parent dirs

269

(.getAbsolutePath f) ; => "/current/path/to/file.txt"

270

271

;; Copying data

272

(with-open [in (clojure.java.io/input-stream "source.txt")

273

out (clojure.java.io/output-stream "dest.txt")]

274

(clojure.java.io/copy in out))

275

276

;; Reading from URL

277

(with-open [rdr (clojure.java.io/reader "https://example.com/data.json")]

278

(slurp rdr))

279

280

;; Processing large files line by line

281

(with-open [rdr (clojure.java.io/reader "large-file.txt")]

282

(doseq [line (line-seq rdr)]

283

(process-line line)))

284

285

;; Print functions

286

(println "Hello" "World") ; Prints: Hello World

287

(printf "Name: %s, Age: %d\n" "Alice" 30) ; Prints: Name: Alice, Age: 30

288

(pr {:name "Alice" :age 30}) ; Prints: {:name "Alice", :age 30}

289

290

;; Reading input

291

(print "Enter your name: ")

292

(flush)

293

(def name (read-line))

294

295

;; Dynamic binding for output redirection

296

(with-open [wtr (clojure.java.io/writer "output.log")]

297

(binding [*out* wtr]

298

(println "This goes to the file")

299

(prn {:data "structure"})))

300

301

;; Controlling print behavior

302

(binding [*print-length* 3]

303

(pr (range 10))) ; Prints: (0 1 2 ...)

304

```