API for io (master branch)

by Stuart Sierra

Usage:
(ns your-namespace
  (:require clojure.contrib.io))

Overview

This file defines polymorphic I/O utility functions for Clojure.

The Streams protocol defines reader, writer, input-stream and
output-stream methods that return BufferedReader, BufferedWriter,
BufferedInputStream and BufferedOutputStream instances (respectively),
with default implementations extended to a variety of argument
types: URLs or filenames as strings, java.io.File's, Sockets, etc.

Public Variables and Functions



*append*

var

  
If true, writer, output-stream and spit will open files in append mode.
Defaults to false.  Instead of binding this var directly, use append-writer,
append-output-stream or append-spit.
Source


*buffer-size*

var

  
Size, in bytes or characters, of the buffer used when
copying streams.
Source


*byte-array-type*

var

  
Type object for a Java primitive byte array.
Source


*char-array-type*

var

  
Type object for a Java primitive char array.
Source


*default-encoding*

var

  
Name of the default encoding to use when reading & writing.
Default is UTF-8.
Source


append-output-stream

function
Usage: (append-output-stream x)
Like output-stream but opens file for appending.  Does not work on streams
that are already open.
Source


append-spit

function
Usage: (append-spit f content)
Like spit but appends to file.
Source


append-writer

function
Usage: (append-writer x)
Like writer but opens file for appending.  Does not work on streams
that are already open.
Source


as-url

function
Usage: (as-url arg)
Coerces argument (URL, URI, or String) to a java.net.URL.
Source


copy

function
Usage: (copy input output)
Copies input to output.  Returns nil.
Input may be an InputStream, Reader, File, byte[], or String.
Output may be an OutputStream, Writer, or File.

Does not close any streams except those it opens itself 
(on a File).

Writing a File fails if the parent directory does not exist.
Source


delete-file

function
Usage: (delete-file f & [silently])
Delete file f. Raise an exception if it fails unless silently is true.
Source


delete-file-recursively

function
Usage: (delete-file-recursively f & [silently])
Delete file f. If it's a directory, recursively delete all its contents.
Raise an exception if any deletion fails unless silently is true.
Source


file

function
Usage: (file arg)
       (file parent child)
       (file parent child & more)
Returns a java.io.File from string or file args.
Source


file-str

function
Usage: (file-str & args)
Concatenates args as strings and returns a java.io.File.  Replaces
all / and \ with File/separatorChar.  Replaces ~ at the start of
the path with the user.home system property.
Source


input-stream

function
Usage: (input-stream x)
Attempts to coerce its argument into an open java.io.InputStream.
The default implementations of this protocol always return a
java.io.BufferedInputStream.

Default implementations are defined for OutputStream, File, URI, URL,
Socket, byte array, and String arguments.

If the argument is a String, it tries to resolve it first as a URI, then
as a local file name.  URIs with a 'file' protocol are converted to
local file names.

Should be used inside with-open to ensure the InputStream is properly
closed.


make-parents

function
Usage: (make-parents file)
Creates all parent directories of file.
Source


output-stream

function
Usage: (output-stream x)
Attempts to coerce its argument into an open java.io.OutputStream.
The default implementations of this protocol always return a
java.io.BufferedOutputStream.

Default implementations are defined for OutputStream, File, URI, URL,
Socket, and String arguments.

If the argument is a String, it tries to resolve it first as a URI, then
as a local file name.  URIs with a 'file' protocol are converted to
local file names.

Should be used inside with-open to ensure the OutputStream is
properly closed.


pwd

function
Usage: (pwd)
Returns current working directory as a String.  (Like UNIX 'pwd'.)
Note: In Java, you cannot change the current working directory.
Source


read-lines

function
Usage: (read-lines f)
Like clojure.core/line-seq but opens f with reader.  Automatically
closes the reader AFTER YOU CONSUME THE ENTIRE SEQUENCE.
Source


reader

function
Usage: (reader x)
Attempts to coerce its argument into an open java.io.Reader.
The default implementations of this protocol always return a
java.io.BufferedReader.

Default implementations are provided for Reader, BufferedReader,
InputStream, File, URI, URL, Socket, byte arrays, character arrays,
and String.

If argument is a String, it tries to resolve it first as a URI, then
as a local file name.  URIs with a 'file' protocol are converted to
local file names.  If this fails, a final attempt is made to resolve
the string as a resource on the CLASSPATH.

Uses *default-encoding* as the text encoding.

Should be used inside with-open to ensure the Reader is properly
closed.


relative-path-string

multimethod
No usage documentation available
Interpret a String or java.io.File as a relative path string. 
Building block for clojure.contrib.java/file.
Source


slurp*

function
Usage: (slurp* f)
Like clojure.core/slurp but opens f with reader.
Source


spit

function
Usage: (spit f content)
Opposite of slurp.  Opens f with writer, writes content, then
closes f.
Source


to-byte-array

function
Usage: (to-byte-array arg)
Converts argument into a Java byte array.  Argument may be
a String, File, InputStream, or Reader.  If the argument is already
a byte array, returns it.
Source


with-in-reader

macro
Usage: (with-in-reader f & body)
Opens a PushbackReader on f, binds it to *in*, and evaluates body.
Source


with-out-append-writer

macro
Usage: (with-out-append-writer f & body)
Like with-out-writer but appends to file.
Source


with-out-writer

macro
Usage: (with-out-writer f & body)
Opens a writer on f, binds it to *out*, and evalutes body.
Anything printed within body will be written to f.
Source


write-lines

function
Usage: (write-lines f lines)
Writes lines (a seq) to f, separated by newlines.  f is opened with
writer, and automatically closed at the end of the sequence.
Source


writer

function
Usage: (writer x)
Attempts to coerce its argument into an open java.io.Writer.
The default implementations of this protocol always return a
java.io.BufferedWriter.

Default implementations are provided for Writer, BufferedWriter,
OutputStream, File, URI, URL, Socket, and String.

If the argument is a String, it tries to resolve it first as a URI, then
as a local file name.  URIs with a 'file' protocol are converted to
local file names.

Should be used inside with-open to ensure the Writer is properly
closed.
Logo & site design by Tom Hickey.
Clojure auto-documentation system by Tom Faulhaber.