File Management Classes

This article describes the main classes you use to interact with files, volumes, and the Finder.

NSFileManager

You use an NSFileManager object to perform many generic file-system operations—for example you can:

Besides offering a useful range of generic functionality, the NSFileManager class insulates an application from the underlying file system. An important part of this insulation is the encoding of filenames (in, for example, Unicode, ISO Latin1, and ASCII). There is a default NSFileManager object for the file system; this object responds to all messages that request an operation on the associated file system.

The pathnames specified as arguments to NSFileManager methods can be absolute or relative to the current directory (which you can determine with currentDirectoryPath and set with changeCurrentDirectoryPath:). However, pathnames cannot include wildcard characters.

You can set a delegate for a file manager; the delegate allows you to modify the behavior of the manager after various events have occurred. For example, fileManager:shouldProceedAfterError:removingItemAtURL: allows your delegate to intervene in the process of removing an item.

NSWorkspace

You use NSWorkspace to:

NSURL

The NSURL class provides a way to manipulate URLs and the resources they reference. You can use NSURL objects to refer to files, and are the preferred way to do so. Particularly in Mac OS X v10.6 and later, objects that can read data from or write data to a file generally have methods that accept an NSURL object instead of a pathname as the file reference. URL-based operations are typically much more efficient than path-based equivalents.

NSString

You can use NSString objects to represent paths. NSString provides a number of utility methods to allow you to manipulate paths, for example to find the file name or path extension. In Mac OS X v10.6 and later, you should use NSURL objects rather than strings to represent paths—URLs are typically much more efficient.