Re: Factor

Factor: the language, the theory, and the practice.

Manipulating Files

Sunday, September 11, 2011

#files

Java has had historically frustrating API’s for interacting with files. In Java 7, these API’s were cleaned up a little bit. A blog post demonstrates some examples of using these new API’s. I would say, though, that Factor demonstrates a much simpler cross-platform API:

Creating and Deleting Files

To create a file, simply:

IN: scratchpad "/path/to/file" touch-file

To update file permissions (on unix systems), you can use the io.files.info.unix vocabulary:

IN: scratchpad "/path/to/file" OCT: 666 set-file-permissions

Note: it would be nice to support parsing “rw-rw-rw-” and “g+x” and similar permission strings, and probably not very difficult.

To delete a file, simply:

IN: scratchpad "/path/to/file" delete-file

Copying and Moving Files

To copy a file from one path to another:

IN: scratchpad "/path/from" "/path/to" copy-file

To copy a file, and preserve its file permissions:

IN: scratchpad "/path/from" "/path/to" copy-file-and-info

To copy a file into a directory:

IN: scratchpad "/dir1/file" "/dir2" copy-file-into

To move a file from one path to another:

IN: scratchpad "/path/from" "/path/to" move-file

To move a file into a directory:

IN: scratchpad "/dir1/file" "/dir2" move-file-into