Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

nsIFile

An instance of this interface is a cross-platform representation of a location in the filesystem.
继承于: nsISupports 最后修改于Gecko 1.0

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.

Note: nsILocalFile was merged with this interface in Gecko 14. Much of the documentation has not been updated to reflect this change.

Method overview

void append(in AString node);
void appendNative(in ACString node); Native code only!
void appendRelativeNativePath(in ACString relativeFilePath); Native code only!
void appendRelativePath(in AString relativeFilePath);
nsIFile clone();
boolean contains(in nsIFile inFile, in boolean recur);
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); Native code only!
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(); Native code only!
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); Native code only!
PRFileDescStar openNSPRFileDesc(in long flags, in long mode); Native code only!
void remove(in boolean recursive);
void reveal();
void setRelativeDescriptor(in nsIFile fromFile, in ACString relativeDesc);

Attributes

Attribute Type Description
directoryEntries nsISimpleEnumerator Returns an enumeration of the elements in a directory. Each element in the enumeration is an nsIFile. Read only.
Exceptions thrown
NS_ERROR_FILE_NOT_DIRECTORY
Indicates that this nsIFile does not reference a directory.
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 only deals with the size of the data fork. If you need to know the size of the combined data and resource forks use nsILocalFileMac.GetFileSizeWithResFork().

Changing the size of a 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 nsIFile.

Unlike fileSize, this attribute corresponds to the size of the symbolic link referenced by this nsIFile. If this nsIFile does not reference a symbolic link, then the value of this attribute is undefined.

The value of this attribute is the number of bytes corresponding to the data represented by the symbolic link. Any meta data, such as a resource fork on the Mac, is not included in the measurement of the file size. Read only.
followLinks PRBool

Determines whether or not the nsIFile will automatically resolve symbolic links.

By default, this value is 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 nsIFile was last modified.

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 a nsIFile 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 nsIFile was last modified.

Unlike lastModifiedTime, this attribute corresponds to the last modified time of the symbolic link referenced by this nsIFile. If this nsIFile does not reference a symbolic link, then the value of this attribute is undefined.

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 a nsIFile 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 without any directory components.

Changing the leaf name of a nsIFile does not affect the underlying filesystem. It only changes what this nsIFile references.
nativeLeafName ACString

This attribute exposes the name of the nsIFile without any directory components. [native character encoding variant]

Changing the leaf name of a 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 nsIFile. [native character encoding variant]

The value of this attribute is a platform-specific file path. Read only. Native code only!
nativeTarget ACString

This attribute exposes the full target of the nsIFile - the full path with any symbolic links dereferenced. [native character encoding variant]

The value of this attribute is a platform-specific file path. Read only. Native code only!
parent nsIFile

This attribute returns the parent nsIFile for this nsIFile.

The parent will be 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 nsIFile (including the leafName). Read only.

Example, this could return "/home/user/foo.txt" or "C:\Images\my.jpeg"
permissions unsigned long

The value of this attribute is a UNIX-style file permission mask for this nsIFile.

Changing the permissions of a 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 nsIFile.

Unlike permissions, this attribute corresponds to the permissions of the symbolic link referenced by this nsIFile. If this nsIFile does not reference a symbolic link, then the value of this attribute is undefined.

Changing the permissions of a 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 nsIFile needs to be serialized to disk and later recovered. This string is not intended for display to users.

Note: The value of the followLinks attribute is not encoded in the persistent descriptor.
target AString

This attribute exposes the full target of the nsIFile - the full path with any symbolic links dereferenced.

Accessor to the string path. The native version of these strings are not guaranteed to be a usable path to pass to NSPR or the C stdlib. There are problems that affect platforms on which a path does not fully specify a file because two volumes can have the same name (For example Mac). This is solved by holding "private", native data in the nsIFile implementation. This native data is lost when you convert to a string. DO NOT PASS TO USE WITH NSPR OR STDLIB! Read only.

Exceptions thrown
NS_ERROR_FILE_INVALID_PATH
Indicates that this nsIFile does not reference a symbolic links.

Constants

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

Methods

append()

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
);
Parameters
node
A string which is intended to be a child node of the nsIFile. This string must not contain a path separator character.
Exceptions thrown
NS_ERROR_FILE_UNRECOGNIZED_PATH
Indicates that aNode incorrectly contains a path separator character.

Native code only!

appendNative

This method is used for constructing a descendant of the current nsIFile. [native character encoding variant]

void appendNative(
  in ACString node
);
Parameters
node
A string that is intended to be a child node of the current nsIFile. This string must not contain a path separator character. This string must be encoded using the native character encoding.
Exceptions thrown
NS_ERROR_FILE_UNRECOGNIZED_PATH
Indicates that aNode incorrectly contains a path separator character.

clone()

This method creates a clone of the nsIFile. (It does NOT clone the file itself; see the copy methods.)

nsIFile clone();
Parameters

None.

Return value

A new nsIFile instance that corresponds to the same file or directory as this nsIFile.

contains()

This method tests whether or not a nsIFile instance is a descendant of the this nsIFile.

boolean contains(
  in nsIFile inFile,
  in boolean recur
);
Parameters
inFile
The nsIFile to test.
recur
This parameter specifies whether or not subdirectories should be inspected. As of Gecko 1.7, this parameter is ignored and always treated as false by the canonical Local File implementation.
Return value

true if inFile is a descendant of this nsIFile.

copyTo()

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
);
Parameters
newParentDir
This parameter specifies the parent directory to copy the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be copied. This parameter may be empty, in which case the current leaf name will be used.
Exceptions thrown
NS_ERROR_FILE_DESTINATION_NOT_DIR
If the "newParentDir" is not a directory.
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.
NS_ERROR_FILE_ALREADY_EXISTS
Indicates that there is already a file named "newName" in the destination directory.

copyToFollowingLinks()

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
);
Parameters
newParentDir
This parameter specifies the parent directory to copy the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be copied. This parameter may be empty, in which case the current leaf name will be used.
Exceptions thrown
NS_ERROR_FILE_DESTINATION_NOT_DIR
If the "newParentDir" is not a directory.
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.
NS_ERROR_FILE_ALREADY_EXISTS
Indicates that there is already a file named "newName" in the destination directory.

Native code only!

copyToFollowingLinksNative

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
);
Parameters
newParentDir
This parameter specifies the parent directory to copy the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be copied. This parameter may be empty, in which case the current leaf name will be used. This string must be encoded using the native character encoding.
Exceptions thrown
NS_ERROR_FILE_DESTINATION_NOT_DIR
If the "newParentDir" is not a directory.
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.
NS_ERROR_FILE_ALREADY_EXISTS
Indicates that there is already a file named "newName" in the destination directory.

Native code only!

CopyToNative

This method copies this file to a new location. [native character encoding variant]

void CopyToNative(
  in nsIFile newParentDir,
  in ACString newName
);
Parameters
newParentDir
This parameter specifies the parent directory to copy the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be copied. This parameter may be empty, in which case the current leaf name will be used. This string must be encoded using the native character encoding.
Exceptions thrown
NS_ERROR_FILE_DESTINATION_NOT_DIR
If the "newParentDir" is not a directory.
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.
NS_ERROR_FILE_ALREADY_EXISTS
Indicates that there is already a file named "newName" in the destination directory.

create()

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
);
Parameters
type
This specifies the type of file system object to be made. The only two types at this time are file and directory which are defined above.
permissions
A UNIX-style file permissions value. For example, the octal value 0600 may be used to limit read and write access to the current user of the system. This parameter may be ignored on systems that do not support file permissions.
Exceptions thrown
NS_ERROR_FILE_ALREADY_EXISTS
Indicates that the file or directory already exists.
NS_ERROR_FILE_UNKNOWN_TYPE
Indicates that the value of "type" does not correspond to a known type.

createUnique()

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
);
Parameters
type
This specifies the type of file system object to be made. The only two types at this time are file and directory which are defined above.
permissions
A UNIX-style file permissions value. For example, the octal value 0600 may be used to limit read and write access to the current user of the system. This parameter may be ignored on systems that do not support file permissions.
Exceptions thrown
NS_ERROR_FILE_UNKNOWN_TYPE
Indicates that the value of "type" does not correspond to a known type.
NS_ERROR_FILE_TOO_BIG
Indicates that the search for nonexistent files was unsuccessful because all of the attempted leaf name variants already exist.

equals()

This method tests whether or not two nsIFile instances correspond to the same file or directory.

boolean equals(
  in nsIFile inFile
);
Parameters
inFile
The nsIFile to compare this nsIFile against.
Return value

true if "inFile" is equivalent to this nsIFile.

exists()

This method tests whether or not this nsIFile exists.

boolean exists()
Parameters

None.

Return value

true if the file or directory exists. Otherwise it returns false.

isDirectory()

This method tests whether or not this nsIFile corresponds to a directory.

boolean isDirectory();
Parameters

None.

Return value

true if this nsIFile corresponds to a directory. Otherwise it returns false.

isExecutable()

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.

Parameters

None.

Return value

true if the nsIFile may be executed by the user. Otherwise it returns false.

isFile()

This method tests whether or not this nsIFile corresponds to a normal file.

boolean isFile();
Parameters

None.

Return value

true if this nsIFile corresponds to a normal file. Otherwise it returns false.

isHidden()

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();
Parameters

None.

Return value

true if the file or directory is hidden. Otherwise it returns false.

isReadable()

This method tests whether or not this nsIFile corresponds to a file or directory that may be read by the user.

boolean isReadable();
Parameters

None.

Return value

true if the file or directory may be read by the user. Otherwise it returns false.

isSpecial()

This method tests whether or not this nsIFile corresponds to a special system file.

Note: The definition of a special system file is platform dependent. For example, under UNIX platforms this might correspond to a device file, socket, or fifo.

boolean isSpecial();
Parameters

None.

Return value

true if this nsIFile corresponds to a special system file. Otherwise it returns false.

isSymlink()

This method tests whether or not this nsIFile corresponds to a symbolic link, shortcut, or alias.

boolean isSymlink();
Parameters

None.

Return value

true if this nsIFile corresponds to a symbolic link. Otherwise it returns false.

isWritable()

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();
Parameters

None.

Return value

true if the file or directory may be modified by the user. Otherwise it returns false.

moveTo()

This method moves this file to a new location.

Note: If this method succeeds, this instance will be updated to point to the new file.

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:

  • This process may take a long time if the file is large and/or the bandwidth is narrow.
  • If the copyTo() method loses data, such as Mac resource data, then so will such a move.
  • If attribute data cannot be recreated (like the file owner if you don't have enough privileges, or ACL data if moving to a non-ACL volume), then those attributes will be lost.
  • The new file's serial number will almost certainly be different, because it is a different file, probably stored in a different physical location.
void moveTo(
  in nsIFile newParentDir,
  in AString newName
);
Parameters
newParentDir
This parameter specifies the parent directory to move the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be moved. This parameter may be empty, in which case the current leaf name will be used.
Exceptions thrown
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to move a file that does not exist.
NS_ERROR_FILE_DIR_NOT_EMPTY
Indicates that an attempt was made to move a directory to the location of an existing directory that is not empty.
NS_ERROR_FILE_ACCESS_DENIED
Indicates that an attempt was made to move a directory to the location of an existing directory that is not writable.
NS_ERROR_FILE_DESTINATION_NOT_DIR
Indicates that "newParentDir" exists and is not a directory.

Native code only!

moveToNative

This method moves this file to a new location. [native character encoding variant]

Note: If this method succeeds, this instance will be updated to point to the new file.

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:

  • This process may take a long time if the file is large and/or the bandwidth is narrow.
  • If the copyTo() method loses data, such as Mac resource data, then so will such a move.
  • If attribute data cannot be recreated (like the file owner if you don't have enough privileges, or ACL data if moving to a non-ACL volume), then those attributes will be lost.
  • The new file's serial number will almost certainly be different, because it is a different file, probably stored in a different physical location.
void moveToNative(
  in nsIFile newParentDir,
  in ACString newName
);
Parameters

 

newParentDir
This parameter specifies the parent directory to copy the file into. If this parameter is null, then the parent directory of the file will be used.
newName
This parameter allows you to specify a new leaf name for the file to be copied. This parameter may be empty, in which case the current leaf name will be used. This string must be encoded using the native character encoding.
Exceptions thrown
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to move a file that does not exist.
NS_ERROR_FILE_DIR_NOT_EMPTY
Indicates that an attempt was made to move a directory to the location of an existing directory that is not empty.
NS_ERROR_FILE_ACCESS_DENIED
Indicates that an attempt was made to move a directory to the location of an existing directory that is not writable.
NS_ERROR_FILE_DESTINATION_NOT_DIR
Indicates that "newParentDir" exists and is not a directory.

normalize()

This method is used to canonicalize the path represented by this nsIFile. (for example 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();
Parameters

None.

Exceptions thrown
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the file path does not exist.
NS_ERROR_FILE_DESTINATION_NOT_DIR
Indicates that a component of the path prefix is not a directory.
NS_ERROR_FILE_ACCESS_DENIED
Read or search permission was denied for a component of the path prefix.

remove()

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
);
Parameters
recursive
If this 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.
Exceptions thrown
NS_ERROR_FILE_TARGET_DOES_NOT_EXIST
Indicates that the current file path does not exist. It is not possible to remove a file that does not exist.
NS_ERROR_FILE_DIR_NOT_EMPTY
Indicates that an attempt was made to remove a directory that is not empty.
NS_ERROR_FILE_ACCESS_DENIED
Indicates that an attempt was made to remove a file in a way that exceeded your permissions. Details depend on your file system and how its permissions work.

Remarks

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.

See also

文档标签和贡献者

 最后编辑者: ziyunfei,