nsISupports
Last changed in Gecko 30.0 (Firefox 30.0 / Thunderbird 30.0 / SeaMonkey 2.27)nsIFile
is the correct platform-agnostic way to specify a file; you should always use this instead of a string to ensure compatibility.
With an nsIFile
you can navigate to ancestors or descendants without having to deal with the different path separators used on different platforms, query the state of any file or directory at the position represented by the nsIFile
and create, move or copy items in the filesystem.
An nsIFile
can be retrieved by either instantiating an nsILocalFile
using a platform specific path string or by using cross-platform locations retrieved from the directory service.
All methods with string parameters have two forms. The preferred form operates on UTF-16 encoded characters strings. An alternate form operates on characters strings encoded in the "native" charset. A string containing characters encoded in the native charset cannot be safely passed to javascript via xpconnect. Therefore, the UTF-16 forms are scriptable, but the "native methods" are not. In addition, the native form cannot deal with files whose name contains characters outside the default system code page on Windows. Using the native form limits the ability of your code to deal with the full Unicode support on Windows 2000 or later where the OS itself does not have such a limitation. Therefore, you must not use the native form unless it is guaranteed that the path passed to a native form function is always ASCII.
To create an nsIFile from a path you can use FileUtils.jsm:
var FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils
var nsifile = new FileUtils.File( fileName )
nsILocalFile
was merged with this interface in Gecko 14. Much of the documentation has not been updated to reflect this change.void append(in AString node); |
void appendNative(in ACString node); Native code only! |
void appendRelativeNativePath(in ACString relativeFilePath); |
void appendRelativePath(in AString relativeFilePath); |
nsIFile clone(); |
boolean contains(in nsIFile inFile); |
void copyTo(in nsIFile newParentDir, in AString newName); |
void copyToFollowingLinks(in nsIFile newParentDir, in AString newName); |
void copyToFollowingLinksNative(in nsIFile newParentDir, in ACString newName); Native code only! |
void CopyToNative(in nsIFile newParentDir, in ACString newName); Native code only! |
void create(in unsigned long type, in unsigned long permissions); |
void createUnique(in unsigned long type, in unsigned long permissions); |
boolean equals(in nsIFile inFile); |
boolean exists(); |
ACString getRelativeDescriptor(in nsIFile fromFile); |
void initWithFile(in nsIFile aFile); |
void initWithNativePath(in ACString filePath); |
void initWithPath(in AString filePath); |
boolean isDirectory(); |
boolean isExecutable(); |
boolean isFile(); |
boolean isHidden(); |
boolean isReadable(); |
boolean isSpecial(); |
boolean isSymlink(); |
boolean isWritable(); |
void launch(); |
PRLibraryStar load(); |
void moveTo(in nsIFile newParentDir, in AString newName); |
void moveToNative(in nsIFile newParentDir, in ACString newName); Native code only! |
void normalize(); |
FILE openANSIFileDesc(in string mode); |
PRFileDescStar openNSPRFileDesc(in long flags, in long mode); |
void renameTo(in nsIFile newParentDir, in AString newName); |
void remove(in boolean recursive); |
void reveal(); |
void setRelativeDescriptor(in nsIFile fromFile, in ACString relativeDesc); |
Attribute | Type | Description |
directoryEntries |
|
Returns an enumeration of the elements in a directory. Each element in the enumeration is an nsIFile . Read only.
Exceptions thrown
|
diskSpaceAvailable |
PRInt64 |
The number of bytes available to non-superuser on the disk volume containing the nsIFile . Read only. |
fileSize |
PRInt64 |
The value of this attribute is the number of bytes corresponding to the data represented by the file. On the Mac, getting/setting the file size with nsIFile operates on the underlying filesystem, possibly truncating the existing file to specified size. |
fileSizeOfLink |
PRInt64 |
This attribute exposes the size of the symbolic link referenced by this Unlike |
followLinks |
PRBool |
Determines whether or not the false on all non-UNIX systems. As of Mozilla 1.7, this attribute is ignored on UNIX systems. |
lastModifiedTime |
PRInt64 |
This attribute exposes the time when the file referenced by this The value of this attribute is milliseconds since midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT). Changing the last modified time of ansIFile operates on the underlying filesystem. As of Gecko 1.7, changing the last modified time of a non-existent file has undefined behavior. |
lastModifiedTimeOfLink |
PRInt64 |
This attribute exposes the time when the symbolic link referenced by this Unlike The value of this attribute is milliseconds since midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT). Changing the last modified time of ansIFile operates on the underlying filesystem. As of Gecko 1.7, changing the last modified time of a non-existent file has undefined behavior. |
leafName |
AString |
This attribute exposes the name of the nsIFile does not affect the underlying filesystem. It only changes what this nsIFile references. |
nativeLeafName |
ACString |
This attribute exposes the name of the nsIFile does not affect the underlying filesystem. It only changes what this nsIFile references. Native code only! |
nativePath |
ACString |
This attribute exposes the full path of the |
nativeTarget |
ACString |
This attribute exposes the full target of the |
parent |
|
This attribute returns the parent null when this nsIFile references the top of the volume. For example, C:\ does not have a parent. Read only. |
path |
AString |
This attribute exposes the full platform-specific path of the |
permissions |
unsigned long |
The value of this attribute is a UNIX-style file permission mask for this nsIFile operates on the underlying filesystem. As of Gecko 1.7, changing the permissions of a non-existent file has undefined behavior. |
permissionsOfLink |
unsigned long |
The value of this attribute is a UNIX-style file permission mask for this Unlike nsIFile operates on the underlying filesystem. As of Gecko 1.7, changing the permissions of a non-existent file has undefined behavior. |
persistentDescriptor |
ACString |
On some platforms, the value of nsIFile.path may be insufficient to uniquely identify the file on the local file system. The persistent descriptor is intended to be used whenever a Note: The value of the
followLinks attribute is not encoded in the persistent descriptor. |
target |
AString |
This attribute exposes the full target of the Accessor to the string Exceptions thrown
|
Constant | Value | Description |
NORMAL_FILE_TYPE |
0 |
A normal file. |
DIRECTORY_TYPE |
1 |
A directory/folder. |
DELETE_ON_CLOSE |
0x80000000 |
Optional parameter used by openNSPRFileDesc |
This function is used for constructing a descendant of the current nsIFile
.
Note: This method does not return a new nsIFile
; it modifies the object it was called on. If the old nsIFile
should be retained, use clone()
.
void append( in AString node );
node
nsIFile
. This string must not contain a path separator character.NS_ERROR_FILE_UNRECOGNIZED_PATH
This method is used for constructing a descendant of the current nsIFile
. [native character encoding variant]
void appendNative( in ACString node );
node
nsIFile
. This string must not contain a path separator character. This string must be encoded using the native character encoding.NS_ERROR_FILE_UNRECOGNIZED_PATH
This method creates a clone of the nsIFile
. (It does NOT clone the file itself; see the copy methods.)
nsIFile clone();
None.
A new nsIFile
instance that corresponds to the same file or directory as this nsIFile
.
This method tests whether or not the path represented by the specified nsIFile
instance is a descendant of the path represented by this nsIFile
. This nsIFile
should point to a directory, while the specified argument can point to a file or directory.
boolean contains( in nsIFile inFile );
inFile
nsIFile
to test.true
if inFile
is a descendant of this nsIFile
. inFile
does not need to be a direct child of this nsIFile
to be considered a descendant.
This method copies a source file to a new location if it does not already exist.
This method will NOT resolve aliases/shortcuts during the copy.
void copyTo( in nsIFile newParentDir, in AString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_DESTINATION_NOT_DIR
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_ALREADY_EXISTS
This method copies a source file to a new location if it does not already exist.
This method is identical to copyTo()
except that any symbolic links will be followed as the name suggests.
void copyToFollowingLinks( in nsIFile newParentDir, in AString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_DESTINATION_NOT_DIR
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_ALREADY_EXISTS
This method copies this file to a new location. [native character encoding variant]
This method is identical to copyToNative()
except that any symbolic links will be followed as the name suggests.
void copyToFollowingLinksNative( in nsIFile newParentDir, in ACString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_DESTINATION_NOT_DIR
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_ALREADY_EXISTS
This method copies this file to a new location. [native character encoding variant]
void CopyToNative( in nsIFile newParentDir, in ACString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_DESTINATION_NOT_DIR
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_ALREADY_EXISTS
This method creates a new file or directory in the file system corresponding to the file path represented by this nsIFile
. This method will create any path segments that do not already exist.
void create( in unsigned long type, in unsigned long permissions );
type
permissions
NS_ERROR_FILE_ALREADY_EXISTS
NS_ERROR_FILE_UNKNOWN_TYPE
This function will create a new file or directory in the file system. Any nodes that have not been created or resolved, will be. If this file already exists, we try variations on the leaf name "suggestedName" until we find one that did not already exist. This method will create any path segments that do not already exist.
void createUnique( in unsigned long type, in unsigned long permissions );
type
permissions
NS_ERROR_FILE_UNKNOWN_TYPE
NS_ERROR_FILE_TOO_BIG
This method tests whether or not two nsIFile
instances correspond to the same file or directory.
boolean equals( in nsIFile inFile );
inFile
nsIFile
to compare this nsIFile
against.true
if "inFile" is equivalent to this nsIFile
.
This method tests whether or not this nsIFile
exists.
boolean exists()
None.
true
if the file or directory exists. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a directory.
boolean isDirectory();
None.
true
if this nsIFile
corresponds to a directory. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a file that may be executed.
Note: This method does not work on all platforms due to bug 322865.
boolean isExecutable();
Note: Use nsIProcess
to then execute/run this file.
None.
true
if the nsIFile
may be executed by the user. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a normal file.
boolean isFile();
None.
true
if this nsIFile
corresponds to a normal file. Otherwise it returns false
.
This method will throw NS_ERROR_FILE_NOT_FOUND
if the path is invalid. To avoid this, check with nsIFile.exists()
first.
This method tests whether or not this nsIFile
corresponds to a file or directory that is hidden. In Unix, hidden files start with a period. On Mac and Windows, they have an attribute bit set.
boolean isHidden();
None.
true
if the file or directory is hidden. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a file or directory that may be read by the user.
boolean isReadable();
None.
true
if the file or directory may be read by the user. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a special system file.
boolean isSpecial();
None.
true
if this nsIFile
corresponds to a special system file. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a symbolic link, shortcut, or alias.
boolean isSymlink();
None.
true
if this nsIFile
corresponds to a symbolic link. Otherwise it returns false
.
This method tests whether or not this nsIFile
corresponds to a file or directory that may be modified by the user. As of Gecko 9, files on read only shares will return false. Files that are exclusively opened on Win32 will return true if they are normally writable, and files that don't have write permissions will return false. For specific handling before Gecko 9 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6), please see bug 682571.
boolean isWritable();
None.
true
if the file or directory may be modified by the user. Otherwise it returns false
.
This method moves this file to a new location.
If the current file path corresponds to a regular file (for storage of bytes), and if the new leaf name identifies a regular file that already exists, then this method will overwrite the destination file.
Usually, "move" means to relocate the file to a different directory without changing the file's contents or properties, or in fact the file's serial number (inode). That is a very fast operation because it just changes directory information. The actual data doesn't move.
Unfortunately, an actual "move" is impossible between different volumes (disks or partitions). This method attempts to do the "right thing" when moving files across volumes. That is, it will copy the old file to the new location, try to assign the file attributes as the old file had them, and then delete the old file. You should be aware of these possible problems:
copyTo()
method loses data, such as Mac resource data, then so will such a move.If you do not want to move file between different volumes, use renameTo instead.
void moveTo( in nsIFile newParentDir, in AString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_DIR_NOT_EMPTY
NS_ERROR_FILE_ACCESS_DENIED
NS_ERROR_FILE_DESTINATION_NOT_DIR
This method moves this file to a new location. [native character encoding variant]
If the current file path corresponds to a regular file (for storage of bytes), and if the new leaf name identifies a regular file that already exists, then this method will overwrite the destination file.
Usually, "move" means to relocate the file to a different directory without changing the file's contents or properties, or in fact the file's serial number (inode). That is a very fast operation because it just changes directory information. The actual data doesn't move.
Unfortunately, an actual "move" is impossible between different volumes (disks or partitions). This method attempts to do the "right thing" when moving files across volumes. That is, it will copy the old file to the new location, try to assign the file attributes as the old file had them, and then delete the old file. You should be aware of these possible problems:
copyTo()
method loses data, such as Mac resource data, then so will such a move.void moveToNative( in nsIFile newParentDir, in ACString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_DIR_NOT_EMPTY
NS_ERROR_FILE_ACCESS_DENIED
NS_ERROR_FILE_DESTINATION_NOT_DIR
This method is used to canonicalize the path represented by this nsIFile
. (for example by resolving symlinks as well as removing .. and . components on Unix).
As of Gecko 1.7, this method is only implemented under UNIX builds (except for Mac OSX). This method will fail if the path does not exist.
void normalize();
None.
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_DESTINATION_NOT_DIR
NS_ERROR_FILE_ACCESS_DENIED
This method moves this file to a new location within the same volume.
This method is identical to moveTo except that if this file or directory is moved to a a different volume, it fails and if this method succeeds, this instance will not updated to point to the new file.
void renameTo( in nsIFile newParentDir, in AString newName );
newParentDir
null
, then the parent directory of the file will be used.newName
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_DIR_NOT_EMPTY
NS_ERROR_FILE_ACCESS_DENIED
NS_ERROR_FILE_DESTINATION_NOT_DIR
This method removes the file or directory corresponding to the file path represented by this nsIFile
.
This method will not resolve any symlinks.
void remove( in boolean recursive );
recursive
nsIFile
corresponds to a directory that is not empty, then this parameter must be true
in order for the directory to be deleted. Otherwise, this parameter is ignored.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
NS_ERROR_FILE_DIR_NOT_EMPTY
NS_ERROR_FILE_ACCESS_DENIED
All string-valued methods and attributes have two forms. The preferred form operates on UTF-16 (Unicode) encoded character strings. The alternate form operates on character strings encoded in the "native" multibyte character set. The native character encoding is defined as the single-byte character encoding used with the standard fopen function on the host system.
The native character encoding is determined using platform specific methods. As of Gecko 1.7, it is UTF-8 on Mac OS X. On Linux and other UNIX platforms, it is the value returned from nl_langinfo (CODESET), which usually corresponds to the value of the LC_ALL, LC_CTYPE and LANG environment variables (with the precedence the same as the order they're enumerated). On Win32 platforms, it is the currently selected ANSI codepage (specified by CP_ACP).
The word "Native" appears in the name of methods that operate on or return strings encoded in the native character set.
A string containing characters encoded in the native character set cannot be safely passed to JavaScript via XPConnect. Therefore, the "native" methods and attributes are not scriptable.
XPCOM provides the string conversion functions NS_CStringToUTF16 and NS_UTF16ToCString, which can be used to convert a string between UTF-16 and the native character encoding.
This interface was frozen for Gecko 1.0. See bug 129279 for details. From Gecko 2.0 interfaces are no longer frozen.