< Previous PageNext Page > Hide TOC

File Manager

This article describes the NSFileManager class and how you use it.

Contents:

Overview
Broken Links
Path Utilities


Overview

NSFileManager enables you to perform many generic file-system operations. With it you can:

Besides offering a useful range of generic functionality, the NSFileManager API insulates an application from the underlying file system. An important part of this insulation is the encoding of file names (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 a 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.

Note: An absolute pathname starts with the root directory of the file system, represented by a slash (/), and ends with the file or directory that the pathname identifies. A relative pathname is relative to the current directory, the directory in which you are working and in which saved files are currently stored (if no pathname is specified). Relative pathnames start with a subdirectory of the current directory—without an initial slash—and end with the name of the file or directory the pathname identifies.

Broken Links

Constructing a pathname to a file does not guarantee that the file exists at that path. Specifying a path results in one of the following possibilities:

If the pathname specifies a valid file or link, you can obtain information about the file using the methods of this class. If the pathname specifies a broken link, you can still use fileAttributesAtPath:traverseLink: to obtain attributes for the link itself (by specifying NO for the traverseLink argument). However, the methods fileExistsAtPath: and fileAttributesAtPath:traverseLink: (with YES specified for the traverseLink argument) return nil when the pathname specifies a broken link. Other methods return appropriate errors—see the method descriptions for specific information. Regardless of whether a link is broken or valid, the link still appears in directory listings.

Path Utilities

NSFileManager methods are commonly used together with path-utility methods implemented as a category on NSString. These methods extract the components of a path (directory, file name, and extension), create paths from those components, “translate” path separators, clean up paths containing symbolic links and redundant slashes, and perform similar tasks. Where your code manipulates strings that are part of file-system paths, it should use these methods. See the specification of the NSString class for details.



< Previous PageNext Page > Hide TOC


© 1997, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-03-05)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.