Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Package | com.apple.cocoa.foundation |
Companion guide |
The NSPathUtilities class provides numerous methods for manipulating file paths. The capabilities include dividing file paths into individual path components, combining individual path components into a full file path, obtaining paths to standard locations in the file system, and retrieving and setting file attributes.
displayNameAtPath
isAbsolutePath
lastPathComponent
pathComponents
pathExtension
pathWithComponents
pathsMatchingExtensions
stringByAbbreviatingWithTildeInPath
stringByAppendingPathComponent
stringByAppendingPathExtension
stringByDeletingLastPathComponent
stringByDeletingPathExtension
stringByExpandingTildeInPath
stringByResolvingSymlinksInPath
stringByStandardizingPath
stringsByAppendingPaths
Creates a new NSPathUtilities object.
public NSPathUtilities
()
All the NSPathUtilities methods are static, so there is no need to create instances of NSPathUtilities.
Returns the name of the file or directory at path in a form appropriate for presentation to the user.
public static String displayNameAtPath
(String path)
Returns an NSDictionary containing various objects that represent the POSIX attributes of the file specified at path.
public static NSDictionary fileAttributes
(String path, boolean flag)
You access these objects using the keys described in the “Constants” section.
If flag is true
and path is a symbolic link, the attributes of the linked-to file are returned; if the link points to a nonexistent file, this method returns null
. If flag is false
, the attributes of the symbolic link are returned.
Interprets aString as a path, returning true
if it represents an absolute path, false
if it represents a relative path.
public static boolean isAbsolutePath
(String aString)
Returns the last path component of aString.
public static String lastPathComponent
(String aString)
The following table illustrates the effect of lastPathComponent
on a variety of different paths:
Value of aString |
String Returned |
---|---|
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“/” |
Interprets aString as a path, returning an array of strings containing, in order, each path component of aString.
public static NSArray pathComponents
(String aString)
The strings in the array appear in the order they did in aString. If aString begins or ends with the path separator, then the first or last component, respectively, contains the separator. Empty components (caused by consecutive path separators) are deleted.
If aString begins with a slash—for example, “/tmp/scratch
”—the array has these contents:
Index |
Path Component |
---|---|
0 |
“ |
1 |
“ |
2 |
“ |
If aString has no separators—for example, “scratch
”—the array contains aString itself, in this case “scratch
”.
Interprets aString as a path, returning the extension of aString, if any (not including the extension divider).
public static String pathExtension
(String aString)
The following table illustrates the effect of pathExtension
on a variety of different paths:
Value of aString |
String Returned |
---|---|
“ |
“ |
“ |
“” (an empty string) |
“ |
“” (an empty string) |
“ |
“ |
Returns the path of aURL conforming to RFC 1808.
public static String pathFromURL
(java.net.URL aURL)
If aURL does not conform to RFC 1808, returns null
.
Returns an array containing all the elements from the pathNames array that have filename extensions from the filterTypes array.
public static NSArray pathsMatchingExtensions
(NSArray pathNames, NSArray filterTypes)
The extensions in filterTypes should not include the dot (“.”) character.
Returns a string built from the strings in components, by concatenating them with a path separator between each pair.
public static String pathWithComponents
(NSArray components)
To create an absolute path, use a slash mark (/
) as the first component. To include a trailing path divider, use an empty string as the last component. This method doesn’t clean up the path created; use stringByStandardizingPath
to resolve empty components, references to the parent directory, and so on.
Creates a list of path strings for the specified directories in the specified domains.
public static NSArray searchPathForDirectoriesInDomains
(int directory, int domainMask, boolean expandTilde)
The list is in the order in which you should search the directories. The list of values for directory and domainMask is described in “Constants.” If expandTilde is true
, tildes are expanded as described in stringByExpandingTildeInPath
.
Changes the attributes of the file or directory specified by path.
public static boolean setFileAttributes
(String path, NSDictionary attributes)
The attributes that you can change include the owner, group, file permissions, and modification date. (The list of file attribute keys are given in the “Constants” section; their values cannot all be changed, however.) As in the POSIX standard, the application either must own the file or directory or must be running as superuser for attribute changes to take effect. The method attempts to make all changes specified in attributes and ignores any rejection of an attempted modification. If all changes succeed, it returns true
. If any change fails, the method returns false
, but it is undefined whether any changes actually occurred.
The FilePosixPermissions
value must be initialized with the code representing the POSIX file-permissions bit pattern. FileHFSCreatorCode
and FileHFSTypeCode
will only be heeded when path specifies a file.
You can change single attributes or any combination of attributes; you need not specify keys for all attributes.
Returns a string representing aString as a path, with a tilde, “~
”, substituted for the full path to the current user’s home directory, or “~user
” for a user other than the current user.
public static String stringByAbbreviatingWithTildeInPath
(String aString)
Returns aString unaltered if it doesn’t begin with the user’s home directory.
Returns a string made by appending aString1 with aString2, preceded if necessary by a path separator.
public static String stringByAppendingPathComponent
(String aString1, String aString2)
The following table illustrates the effect of this method on a variety of different paths, assuming that aString2 is supplied as “scratch.tiff
”:
Value of aString1 |
Resulting String |
---|---|
“ |
“ |
“ |
“ |
“ |
“ |
“” (an empty string) |
“ |
This method may fail to append if PATH_MAX
is exceeded. Its failure mode is to return the original string passed to the method as string1, rather than a string with anything appended to it. The method also logs in console.log
the fact that the attempt failed.
Returns a string made by appending to aString1 an extension separator followed by aString2.
public static String stringByAppendingPathExtension
(String aString1, String aString2)
The following table illustrates the effect of this method on a variety of different paths, assuming that aString2 is supplied as "tiff"
:
Value of aString |
Resulting String |
---|---|
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
Returns a string made by deleting the last path component from aString, along with any final path separator.
public static String stringByDeletingLastPathComponent
(String aString)
If aString represents the root path, however, it’s returned unaltered. The following table illustrates the effect of this method on a variety of different paths:
Value of aString |
Resulting String |
---|---|
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“” (an empty string) |
Returns a string made by deleting the extension (if any, and only the last) from aString.
public static String stringByDeletingPathExtension
(String aString)
Strips any trailing path separator before checking for an extension. If aString represents the root path, however, it’s returned unaltered. The following table illustrates the effect of this method on a variety of different paths:
Value of aString |
Resulting String |
---|---|
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“ |
“. |
“” (an empty string) |
“ |
“ |
Returns a string made by expanding the initial component of aString, if it begins with “~
” or “~user
”, to its full path value.
public static String stringByExpandingTildeInPath
(String aString)
Returns aString unaltered if that component can’t be expanded.
Expands an initial tilde expression in aPath, then resolves all symbolic links and references to current or parent directories if possible, returning a standardized path.
public static String stringByResolvingSymlinksInPath
(String aPath)
If the original path is absolute, all symbolic links are guaranteed to be removed; if it’s a relative path, symbolic links that can’t be resolved are left unresolved in the returned string. Returns this
if an error occurs.
If the name of aPath begins with /private
, this method strips off the /private
designator, provided the result is the name of an existing file.
Returns a string representing the path of aString, with extraneous path components removed.
public static String stringByStandardizingPath
(String aString)
If stringByStandardizingPath
detects symbolic links in a pathname, the stringByResolvingSymlinksInPath
method is called to resolve them. If an invalid pathname is provided, stringByStandardizingPath
may attempt to resolve it by calling stringByResolvingSymlinksInPath
, and the results are undefined. If any other kind of error is encountered (such as a path component not existing), this
is returned.
This method can make the following changes in the provided string:
Expand an initial tilde expression using stringByExpandingTildeInPath
.
Reduce empty components and references to the current directory (that is, the sequences “//” and “/./”) to single path separators.
In absolute paths only, resolve references to the parent directory (that is, the component “..”) to the real parent directory if possible using stringByResolvingSymlinksInPath
, which consults the file system to resolve each potential symbolic link.
In relative paths, because symbolic links can’t be resolved, references to the parent directory are left in place.
Remove an initial component of “/private
” from the path if the result still indicates an existing file or directory (checked by consulting the file system).
Returns an array of strings made by separately appending each string in paths to aString, preceded if necessary by a path separator.
public static NSArray stringsByAppendingPaths
(String aString, NSArray paths)
See stringByAppendingPathComponent
for an individual example.
Returns the path to the system’s temporary directory.
public static String temporaryDirectory
()
Returns a newly created URL referencing the file or directory at path.
public static java.net.URL URLWithPath
(String path)
The following constants for directory locations are provided by NSPathUtilities:
The following masks are provided for working with paths:
The following are keys used to access attributes of a mounted file system:
Key |
Value Type |
---|---|
|
int |
|
int |
|
int |
|
int |
|
int |
The following are keys used to access file attributes contained in the attribute dictionaries returned from fileAttributes
and passed to setFileAttributes
:
Key |
Value Type |
---|---|
int |
|
NSDate |
|
String |
|
String |
|
int |
|
int |
|
int |
|
String (see below for possible values) |
|
boolean |
|
int (see “HFS File Types”) |
|
int (see “HFS File Types”) |
|
|
int |
|
boolean |
|
boolean |
|
NSDate |
|
int |
|
int |
The following constants are the possible values for the FileType
attribute key:
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)