Provide an easy way to read from or write to files in Common Lisp. The contents of the file are placed into a sequence, whose type can be specified, with an optionally specified element type as well. The contents of a sequence can also be stored into a file, with an optionally specified element type.
2006-10-02 | Jonathan Lee | Original version 0.01 of slurp-file function. |
2008-10-15 | Jonathan Lee | Original version 0.01 of spit-file function. |
2011-08-23 | Jonathan Lee | Combined the two file I/O functions into a single package for ease of reference. |
2013-12-20 | Jonathan Lee | Improved documentation. Added markdown README file for Github repository default page. |
To load this project, use QuickLisp, ASDF or simply load the file-io.lisp file in your Lisp REPL.
(ql:quickload "file-io")
or
(asdf:oos 'asdf:load-file "file-io")
or
(load "/path/to/project/file-io/file-io.lisp")
The slurp-file
function provides an easy and fast way to retrieve the contents
of a file.
The function is based on a series of tests designed by Gene Michael Stover to discover the fastest method of retrieving data from a file.
I have extended the function to allow the caller to specify what type of sequence to return and what element type is used to extract the file contents.
(file-io:slurp-file "/pathname/to/your/file")
(file-io:slurp-file "/pathname/to/your/file" :seq-type :array :element-type 'integer)
(file-io:slurp-file "/pathname/to/your/file" :seq-type :list :element-type '(byte 8))
The spit-file
function provides an easy and fast way to create a file from the
contents of a sequence.
(file-io:spit-file "File contents sequence" "/pathname/to/your/file")
(file-io:spit-file #(72 101 108 108 111) "/pathname/to/your/file" :element-type 'integer)
(file-io:spit-file (list 67 111 110 116 101 110 116 115)
"/pathname/to/your/file"
:element-type '(byte 8)
:file-exists :append)
MIT. See "LICENSE".