Retired Document
Important: This document is replaced by File System Programming Guide.
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:
Create directories and files.
Extract the contents of files (as
NSData
objects).Change your current working location in the file system.
Copy, move, and link files and directories.
Remove files, links, and directories.
Get and (where appropriate) set the attributes of a file, a directory, or the file system.
Determine the contents of directories.
Compare files and directories for equality.
Make and evaluate symbolic links.
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:
Open, manipulate, and obtain information about files and devices.
Track changes to the file system, devices, and the user database.
Get and set Finder information for files.
Launch applications.
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.
Copyright © 1997, 2011 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2011-05-25