nsIFile

  • 版本网址缩略名: XPCOM_Interface_Reference/nsIFile
  • 版本标题: nsIFile
  • 版本 id: 42889
  • 创建于:
  • 创建者: ziyunfei
  • 是否是当前版本?
  • 评论 page created, 4135 words added

修订内容

{{ IFSummary("xpcom/io/nsIFile.idl", "nsISupports", "Scriptable", "1.0", "An instance of this interface is a cross-platform representation of a location in the filesystem.") }}

This is the only correct cross-platform way to specify a file. Strings are not such a way. If you grew up on windows or unix, you may think they are. Welcome to reality.

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 {{ interface("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.

Method overview

void append(in AString node);
void appendNative(in ACString node); {{ noscript_inline() }}
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); {{ noscript_inline() }}
void CopyToNative(in nsIFile newParentDir, in ACString newName); {{ noscript_inline() }}
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();
boolean isDirectory();
boolean isExecutable();
boolean isFile();
boolean isHidden();
boolean isReadable();
boolean isSpecial();
boolean isSymlink();
boolean isWritable();
void moveTo(in nsIFile newParentDir, in AString newName);
void moveToNative(in nsIFile newParentDir, in ACString newName); {{ noscript_inline() }}
void normalize();
void remove(in boolean recursive);

Attributes

Attribute Type Description
directoryEntries {{ Interface("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.
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 {{ ifmethod("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.
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. {{ noscript_inline() }}
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. {{ noscript_inline() }}
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. {{ noscript_inline() }}
parent {{ Interface("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.
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.

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 " .. manch("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.

{{ method_noscript("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 {{ manch("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.

{{ method_noscript("copyToFollowingLinksNative") }}

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

This method is identical to {{ manch("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.

{{ method_noscript("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 " .. interface("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 {{ geckoRelease("9.0") }}, 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.

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 {{ manch("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.

{{ method_noscript("moveToNative") }}

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:

  • This process may take a long time if the file is large and/or the bandwidth is narrow.
  • If the {{ manch("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

{{ languages( { "zh-cn": "zh-cn/XPCOM_Interface_Reference/nsILocalFile" } ) }}

修订版来源

<p>{{ IFSummary("xpcom/io/nsIFile.idl", "nsISupports", "Scriptable", "1.0", "An instance of this interface is a cross-platform representation of a location in the filesystem.") }}</p>
<p>This is the only correct cross-platform way to specify a file. Strings are not such a way. If you grew up on windows or unix, you may think they are. Welcome to reality.</p>
<p>With an <code>nsIFile</code> 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 <code>nsIFile</code> and create, move or copy items in the filesystem.</p>
<p>An <code>nsIFile</code> can be retrieved by either instantiating an {{ interface("nsILocalFile") }} using a platform specific path string or by using cross-platform locations retrieved from the <a href="/zh-cn/Code_snippets/File_I//O#Getting_special_files" title="zh-cn/Code_snippets/File_I//O#Getting_special_files">directory service</a>.</p>
<p>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 <strong>cannot</strong> 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 <strong>must not</strong> use the native form unless it is <strong>guaranteed</strong> that the path passed to a native form function is <strong>always</strong> ASCII.</p>
<h2 name="Method_overview">Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><code>void <a href="#append()">append</a>(in AString node);</code></td> </tr> <tr> <td><code>void <a href="#appendNative()">appendNative</a>(in ACString node);</code> {{ noscript_inline() }}</td> </tr> <tr> <td><code>nsIFile <a href="#clone()">clone</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#contains()">contains</a>(in nsIFile inFile, in boolean recur);</code></td> </tr> <tr> <td><code>void <a href="#copyTo()">copyTo</a>(in nsIFile newParentDir, in AString newName);</code></td> </tr> <tr> <td><code>void <a href="#copyToFollowingLinks()">copyToFollowingLinks</a>(in nsIFile newParentDir, in AString newName);</code></td> </tr> <tr> <td><code>void <a href="#copyToFollowingLinksNative()">copyToFollowingLinksNative</a>(in nsIFile newParentDir, in ACString newName);</code> {{ noscript_inline() }}</td> </tr> <tr> <td><code>void <a href="#CopyToNative()">CopyToNative</a>(in nsIFile newParentDir, in ACString newName);</code> {{ noscript_inline() }}</td> </tr> <tr> <td><code>void <a href="#create()">create</a>(in unsigned long type, in unsigned long permissions);</code></td> </tr> <tr> <td><code>void <a href="#createUnique()">createUnique</a>(in unsigned long type, in unsigned long permissions);</code></td> </tr> <tr> <td><code>boolean <a href="#equals()">equals</a>(in nsIFile inFile);</code></td> </tr> <tr> <td><code>boolean <a href="#exists()">exists</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isDirectory()">isDirectory</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isExecutable()">isExecutable</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isFile()">isFile</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isHidden()">isHidden</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isReadable()">isReadable</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isSpecial()">isSpecial</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isSymlink()">isSymlink</a>();</code></td> </tr> <tr> <td><code>boolean <a href="#isWritable()">isWritable</a>();</code></td> </tr> <tr> <td><code>void <a href="#moveTo()">moveTo</a>(in nsIFile newParentDir, in AString newName);</code></td> </tr> <tr> <td><code>void <a href="#moveToNative()">moveToNative</a>(in nsIFile newParentDir, in ACString newName);</code> {{ noscript_inline() }}</td> </tr> <tr> <td><code>void <a href="#normalize()">normalize</a>();</code></td> </tr> <tr> <td><code>void <a href="#remove()">remove</a>(in boolean recursive);</code></td> </tr> </tbody>
</table>
<h2 name="Attributes">Attributes</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Attribute</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>directoryEntries</code></td> <td><code>{{ Interface("nsISimpleEnumerator") }}</code></td> <td>Returns an enumeration of the elements in a directory. Each element in the enumeration is an <code>nsIFile</code>. <strong>Read only.</strong> <h6 name="Exceptions_thrown">Exceptions thrown</h6> <dl> <dt><code>NS_ERROR_FILE_NOT_DIRECTORY</code></dt> <dd>Indicates that this <code>nsIFile</code> does not reference a directory.</dd> </dl> </td> </tr> <tr> <td><code>fileSize</code></td> <td><code><a href="/zh-cn/PRInt64" title="zh-cn/PRInt64">PRInt64</a></code></td> <td> <p>The value of this attribute is the number of bytes corresponding to the data represented by the file.</p> <p>On the Mac, getting/setting the file size with <code>nsIFile</code> only deals with the size of the data fork. If you need to know the size of the combined data and resource forks use {{ ifmethod("nsILocalFileMac","GetFileSizeWithResFork") }}.</p> Changing the size of a <code>nsIFile</code> operates on the underlying filesystem, possibly truncating the existing file to specified size.</td> </tr> <tr> <td><code>fileSizeOfLink</code></td> <td><code><a href="/zh-cn/PRInt64" title="zh-cn/PRInt64">PRInt64</a></code></td> <td> <p>This attribute exposes the size of the symbolic link referenced by this <code>nsIFile</code>.</p> <p>Unlike <code>fileSize</code>, this attribute corresponds to the size of the symbolic link referenced by this <code>nsIFile</code>. If this <code>nsIFile</code> does not reference a symbolic link, then the value of this attribute is undefined.</p> 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. <strong>Read only.</strong></td> </tr> <tr> <td><code>lastModifiedTime</code></td> <td><code><a href="/zh-cn/PRInt64" title="zh-cn/PRInt64">PRInt64</a></code></td> <td> <p>This attribute exposes the time when the file referenced by this <code>nsIFile</code> was last modified.</p> <p>The value of this attribute is milliseconds since midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT).</p> Changing the last modified time of a <code>nsIFile</code> operates on the underlying filesystem. As of {{ gecko("1.7") }}, changing the last modified time of a non-existent file has undefined behavior.</td> </tr> <tr> <td><code>lastModifiedTimeOfLink</code></td> <td><code><a href="/zh-cn/PRInt64" title="zh-cn/PRInt64">PRInt64</a></code></td> <td> <p>This attribute exposes the time when the symbolic link referenced by this <code>nsIFile</code> was last modified.</p> <p>Unlike <code>lastModifiedTime</code>, this attribute corresponds to the last modified time of the symbolic link referenced by this <code>nsIFile</code>. If this <code>nsIFile</code> does not reference a symbolic link, then the value of this attribute is undefined.</p> <p>The value of this attribute is milliseconds since midnight (00:00:00), January 1, 1970 Greenwich Mean Time (GMT).</p> Changing the last modified time of a <code>nsIFile</code> operates on the underlying filesystem. As of {{ gecko("1.7") }}, changing the last modified time of a non-existent file has undefined behavior.</td> </tr> <tr> <td><code>leafName</code></td> <td><code><a href="/zh-cn/AString" title="zh-cn/AString">AString</a></code></td> <td> <p>This attribute exposes the name of the <code>nsIFile</code> without any directory components.</p> Changing the leaf name of a <code>nsIFile</code> does not affect the underlying filesystem. It only changes what this <code>nsIFile</code> references.</td> </tr> <tr> <td><code>nativeLeafName</code></td> <td><code><a href="/zh-cn/ACString" title="zh-cn/ACString">ACString</a></code></td> <td> <p>This attribute exposes the name of the <code>nsIFile</code> without any directory components. [native character encoding variant]</p> Changing the leaf name of a <code>nsIFile</code> does not affect the underlying filesystem. It only changes what this <code>nsIFile</code> references. {{ noscript_inline() }}</td> </tr> <tr> <td><code>nativePath</code></td> <td><code><a href="/zh-cn/ACString" title="zh-cn/ACString">ACString</a></code></td> <td> <p>This attribute exposes the full path of the <code>nsIFile</code>. [native character encoding variant]</p> The value of this attribute is a platform-specific file path. <strong>Read only.</strong> {{ noscript_inline() }}</td> </tr> <tr> <td><code>nativeTarget</code></td> <td><code><a href="/zh-cn/ACString" title="zh-cn/ACString">ACString</a></code></td> <td> <p>This attribute exposes the full target of the <code>nsIFile</code> - the full path with any symbolic links dereferenced. [native character encoding variant]</p> The value of this attribute is a platform-specific file path. <strong>Read only.</strong> {{ noscript_inline() }}</td> </tr> <tr> <td><code>parent</code></td> <td><code>{{ Interface("nsIFile") }}</code></td> <td> <p>This attribute returns the parent <code>nsIFile</code> for this <code>nsIFile</code>.</p> The parent will be <code>null</code> when this <code>nsIFile</code> references the top of the volume. For example, C:\ does not have a parent. <strong>Read only.</strong></td> </tr> <tr> <td><code>path</code></td> <td><code><a href="/zh-cn/AString" title="zh-cn/AString">AString</a></code></td> <td> <p>This attribute exposes the full platform-specific path of the <code>nsIFile</code> (including the <code>leafName</code>). <strong>Read only.</strong></p> Example, this could return "/home/user/foo.txt" or "C:\Images\my.jpeg"</td> </tr> <tr> <td><code>permissions</code></td> <td><code><a href="/zh-cn/unsigned_long" title="zh-cn/unsigned long">unsigned long</a></code></td> <td> <p>The value of this attribute is a UNIX-style file permission mask for this <code>nsIFile</code>.</p> Changing the permissions of a <code>nsIFile</code> operates on the underlying filesystem. As of {{ gecko("1.7") }}, changing the permissions of a non-existent file has undefined behavior.</td> </tr> <tr> <td><code>permissionsOfLink</code></td> <td><code><a href="/zh-cn/unsigned_long" title="zh-cn/unsigned long">unsigned long</a></code></td> <td> <p>The value of this attribute is a UNIX-style file permission mask for this <code>nsIFile</code>.</p> <p>Unlike <code>permissions</code>, this attribute corresponds to the permissions of the symbolic link referenced by this <code>nsIFile</code>. If this <code>nsIFile</code> does not reference a symbolic link, then the value of this attribute is undefined.</p> Changing the permissions of a <code>nsIFile</code> operates on the underlying filesystem. As of {{ gecko("1.7") }}, changing the permissions of a non-existent file has undefined behavior.</td> </tr> <tr> <td><code>target</code></td> <td><code><a href="/zh-cn/AString" title="zh-cn/AString">AString</a></code></td> <td> <p>This attribute exposes the full target of the <code>nsIFile</code> - the full path with any symbolic links dereferenced.</p> <p>Accessor to the string <code>path</code>. The native version of these strings are not guaranteed to be a usable <code>path</code> to pass to NSPR or the C stdlib. There are problems that affect platforms on which a <code>path</code> 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 <code>nsIFile</code> implementation. This native data is lost when you convert to a string. DO NOT PASS TO USE WITH NSPR OR STDLIB! <strong>Read only.</strong></p> <h6 name="Exceptions_thrown">Exceptions thrown</h6> <dl> <dt><code>NS_ERROR_FILE_INVALID_PATH</code></dt> <dd>Indicates that this <code>nsIFile</code> does not reference a symbolic links.</dd> </dl> </td> </tr> </tbody>
</table>
<h2 name="Constants">Constants</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Constant</td> <td class="header">Value</td> <td class="header">Description</td> </tr> <tr> <td><code>NORMAL_FILE_TYPE</code></td> <td><code>0</code></td> <td>A normal file.</td> </tr> <tr> <td><code>DIRECTORY_TYPE</code></td> <td><code>1</code></td> <td>A directory/folder.</td> </tr> </tbody>
</table>
<h2 name="Methods">Methods</h2>
<h3 name="append()">append()</h3>
<p>This function is used for constructing a descendant of the current <code>nsIFile</code>.</p>
<p>{{ note("This method does not return a new <code>nsIFile</code>; it modifies the object it was called on. If the old <code>nsIFile</code> should be retained, use " .. manch("clone") .. ".") }}</p>
<pre class="eval">void append(
  in AString node
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>node</code></dt> <dd>A string which is intended to be a child node of the <code>nsIFile</code>. This string must not contain a path separator character.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_UNRECOGNIZED_PATH</code></dt> <dd>Indicates that aNode incorrectly contains a path separator character.</dd>
</dl>
<p>{{ method_noscript("appendNative") }}</p>
<p>This method is used for constructing a descendant of the current <code>nsIFile</code>. [native character encoding variant]</p>
<pre class="eval">void appendNative(
  in ACString node
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>node</code></dt> <dd>A string that is intended to be a child node of the current <code>nsIFile</code>. This string must not contain a path separator character. This string must be encoded using the native character encoding.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_UNRECOGNIZED_PATH</code></dt> <dd>Indicates that aNode incorrectly contains a path separator character.</dd>
</dl>
<h3 name="clone()">clone()</h3>
<p>This method creates a clone of the <code>nsIFile</code>. (It does NOT clone the file itself; see the copy methods.)</p>
<pre class="eval">nsIFile clone();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p>A new <code>nsIFile</code> instance that corresponds to the same file or directory as this <code>nsIFile</code>.</p>
<h3 name="contains()">contains()</h3>
<p>This method tests whether or not a <code>nsIFile</code> instance is a descendant of the this <code>nsIFile</code>.</p>
<pre class="eval">boolean contains(
  in nsIFile inFile,
  in boolean recur
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>inFile</code></dt> <dd>The <code>nsIFile</code> to test.</dd> <dt><code>recur</code></dt> <dd>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.</dd>
</dl>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if inFile is a descendant of this <code>nsIFile</code>.</p>
<h3 name="copyTo()">copyTo()</h3>
<p>This method copies a source file to a new location if it does not already exist.</p>
<p>This method will NOT resolve aliases/shortcuts during the copy.</p>
<pre class="eval">void copyTo(
  in nsIFile newParentDir,
  in AString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>newParentDir</code></dt> <dd>This parameter specifies the parent directory to copy the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd> <dt><code>newName</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>If the "newParentDir" is not a directory.</dd> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_ALREADY_EXISTS</code></dt> <dd>Indicates that there is already a file named "newName" in the destination directory.</dd>
</dl>
<h3 name="copyToFollowingLinks()">copyToFollowingLinks()</h3>
<p>This method copies a source file to a new location if it does not already exist.</p>
<p>This method is identical to {{ manch("copyTo") }} except that any symbolic links will be followed as the name suggests.</p>
<pre class="eval">void copyToFollowingLinks(
  in nsIFile newParentDir,
  in AString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>newParentDir</code></dt> <dd>This parameter specifies the parent directory to copy the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd> <dt><code>newName</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>If the "newParentDir" is not a directory.</dd> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_ALREADY_EXISTS</code></dt> <dd>Indicates that there is already a file named "newName" in the destination directory.</dd>
</dl>
<p>{{ method_noscript("copyToFollowingLinksNative") }}</p>
<p>This method copies this file to a new location. [native character encoding variant]</p>
<p>This method is identical to {{ manch("copyToNative") }} except that any symbolic links will be followed as the name suggests.</p>
<pre class="eval">void copyToFollowingLinksNative(
  in nsIFile newParentDir,
  in ACString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>newParentDir</code></dt> <dd>This parameter specifies the parent directory to copy the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd> <dt><code>newName</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>If the "newParentDir" is not a directory.</dd> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_ALREADY_EXISTS</code></dt> <dd>Indicates that there is already a file named "newName" in the destination directory.</dd>
</dl>
<p>{{ method_noscript("CopyToNative") }}</p>
<p>This method copies this file to a new location. [native character encoding variant]</p>
<pre class="eval">void CopyToNative(
  in nsIFile newParentDir,
  in ACString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>newParentDir</code></dt> <dd>This parameter specifies the parent directory to copy the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd> <dt><code>newName</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>If the "newParentDir" is not a directory.</dd> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to copy a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_ALREADY_EXISTS</code></dt> <dd>Indicates that there is already a file named "newName" in the destination directory.</dd>
</dl>
<h3 name="create()">create()</h3>
<p>This method creates a new file or directory in the file system corresponding to the file path represented by this <code>nsIFile</code>. This method will create any path segments that do not already exist.</p>
<pre class="eval">void create(
  in unsigned long type,
  in unsigned long permissions
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>type</code></dt> <dd>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.</dd> <dt><code>permissions</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_ALREADY_EXISTS</code></dt> <dd>Indicates that the file or directory already exists.</dd> <dt><code>NS_ERROR_FILE_UNKNOWN_TYPE</code></dt> <dd>Indicates that the value of "type" does not correspond to a known type.</dd>
</dl>
<h3 name="createUnique()">createUnique()</h3>
<p>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.</p>
<pre class="eval">void createUnique(
  in unsigned long type,
  in unsigned long permissions
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>type</code></dt> <dd>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.</dd> <dt><code>permissions</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_UNKNOWN_TYPE</code></dt> <dd>Indicates that the value of "type" does not correspond to a known type.</dd> <dt><code>NS_ERROR_FILE_TOO_BIG</code></dt> <dd>Indicates that the search for nonexistent files was unsuccessful because all of the attempted leaf name variants already exist.</dd>
</dl>
<h3 name="equals()">equals()</h3>
<p>This method tests whether or not two <code>nsIFile</code> instances correspond to the same file or directory.</p>
<pre class="eval">boolean equals(
  in nsIFile inFile
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>inFile</code></dt> <dd>The <code>nsIFile</code> to compare this <code>nsIFile</code> against.</dd>
</dl>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if "inFile" is equivalent to this <code>nsIFile</code>.</p>
<h3 name="exists()">exists()</h3>
<p>This method tests whether or not this <code>nsIFile</code> exists.</p>
<pre class="eval">boolean exists()
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if the file or directory exists. Otherwise it returns <code>false</code>.</p>
<h3 name="isDirectory()">isDirectory()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a directory.</p>
<pre class="eval">boolean isDirectory();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if this <code>nsIFile</code> corresponds to a directory. Otherwise it returns <code>false</code>.</p>
<h3 name="isExecutable()">isExecutable()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a file that may be executed.</p>
<p>{{ note("This method does not work on all platforms due to " .. bug("322865") .. ".") }}</p>
<pre class="eval">boolean isExecutable();
</pre>
<p>{{ note("Use " .. interface("nsIProcess") .. " to then execute/run this file.") }}</p>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if the <code>nsIFile</code> may be executed by the user. Otherwise it returns <code>false</code>.</p>
<h3 name="isFile()">isFile()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a normal file.</p>
<pre class="eval">boolean isFile();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if this <code>nsIFile</code> corresponds to a normal file. Otherwise it returns <code>false</code>.</p>
<h3 name="isHidden()">isHidden()</h3>
<p>This method tests whether or not this <code>nsIFile</code> 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.</p>
<pre class="eval">boolean isHidden();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if the file or directory is hidden. Otherwise it returns <code>false</code>.</p>
<h3 name="isReadable()">isReadable()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a file or directory that may be read by the user.</p>
<pre class="eval">boolean isReadable();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if the file or directory may be read by the user. Otherwise it returns <code>false</code>.</p>
<h3 name="isSpecial()">isSpecial()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a special system file.</p>
<p>{{ 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.") }}</p>
<pre class="eval">boolean isSpecial();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if this <code>nsIFile</code> corresponds to a special system file. Otherwise it returns <code>false</code>.</p>
<h3 name="isSymlink()">isSymlink()</h3>
<p>This method tests whether or not this <code>nsIFile</code> corresponds to a symbolic link, shortcut, or alias.</p>
<pre class="eval">boolean isSymlink();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if this <code>nsIFile</code> corresponds to a symbolic link. Otherwise it returns <code>false</code>.</p>
<h3 name="isWritable()">isWritable()</h3>
<p>This method tests whether or not this <code>nsIFile</code> 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 {{ geckoRelease("9.0") }}, please see {{ Bug("682571") }}.</p>
<pre class="eval">boolean isWritable();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Return_value">Return value</h6>
<p><code>true</code> if the file or directory may be modified by the user. Otherwise it returns <code>false</code>.</p>
<h3 name="moveTo()">moveTo()</h3>
<p>This method moves this file to a new location.</p>
<p>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.</p>
<p>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.</p>
<p>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:</p>
<ul> <li>This process may take a long time if the file is large and/or the bandwidth is narrow.</li> <li>If the {{ manch("copyTo") }} method loses data, such as Mac resource data, then so will such a move.</li> <li>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.</li> <li>The new file's serial number will almost certainly be different, because it is a different file, probably stored in a different physical location.</li>
</ul>
<pre class="eval">void moveTo(
  in nsIFile newParentDir,
  in AString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>newParentDir</code></dt> <dd>This parameter specifies the parent directory to move the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd> <dt><code>newName</code></dt> <dd>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.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to move a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_DIR_NOT_EMPTY</code></dt> <dd>Indicates that an attempt was made to move a directory to the location of an existing directory that is not empty.</dd> <dt><code>NS_ERROR_FILE_ACCESS_DENIED</code></dt> <dd>Indicates that an attempt was made to move a directory to the location of an existing directory that is not writable.</dd> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>Indicates that "newParentDir" exists and is not a directory.</dd>
</dl>
<p>{{ method_noscript("moveToNative") }}</p>
<p>This method moves this file to a new location. [native character encoding variant]</p>
<p>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.</p>
<p>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.</p>
<p>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:</p>
<ul> <li>This process may take a long time if the file is large and/or the bandwidth is narrow.</li> <li>If the {{ manch("copyTo") }} method loses data, such as Mac resource data, then so will such a move.</li> <li>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.</li> <li>The new file's serial number will almost certainly be different, because it is a different file, probably stored in a different physical location.</li>
</ul>
<pre class="eval">void moveToNative(
  in nsIFile newParentDir,
  in ACString newName
);
</pre>
<h6 name="Parameters">Parameters</h6>
<p> </p>
<dt><code>newParentDir</code></dt>
<dd>This parameter specifies the parent directory to copy the file into. If this parameter is <code>null</code>, then the parent directory of the file will be used.</dd>
<dt><code>newName</code></dt>
<dd>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.</dd>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to move a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_DIR_NOT_EMPTY</code></dt> <dd>Indicates that an attempt was made to move a directory to the location of an existing directory that is not empty.</dd> <dt><code>NS_ERROR_FILE_ACCESS_DENIED</code></dt> <dd>Indicates that an attempt was made to move a directory to the location of an existing directory that is not writable.</dd> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>Indicates that "newParentDir" exists and is not a directory.</dd>
</dl>
<h3 name="normalize()">normalize()</h3>
<p>This method is used to canonicalize the path represented by this <code>nsIFile</code>. (for example removing .. and . components on Unix).</p>
<p>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.</p>
<pre class="eval">void normalize();
</pre>
<h6 name="Parameters">Parameters</h6>
<p>None.</p>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the file path does not exist.</dd> <dt><code>NS_ERROR_FILE_DESTINATION_NOT_DIR</code></dt> <dd>Indicates that a component of the path prefix is not a directory.</dd> <dt><code>NS_ERROR_FILE_ACCESS_DENIED</code></dt> <dd>Read or search permission was denied for a component of the path prefix.</dd>
</dl>
<h3 name="remove()">remove()</h3>
<p>This method removes the file or directory corresponding to the file path represented by this <code>nsIFile</code>.</p>
<p>This method will not resolve any symlinks.</p>
<pre class="eval">void remove(
  in boolean recursive
);
</pre>
<h6 name="Parameters">Parameters</h6>
<dl> <dt><code>recursive</code></dt> <dd>If this <code>nsIFile</code> corresponds to a directory that is not empty, then this parameter must be <code>true</code> in order for the directory to be deleted. Otherwise, this parameter is ignored.</dd>
</dl>
<h6 name="Exceptions_thrown">Exceptions thrown</h6>
<dl> <dt><code>NS_ERROR_FILE_TARGET_DOES_NOT_EXIST</code></dt> <dd>Indicates that the current file path does not exist. It is not possible to remove a file that does not exist.</dd> <dt><code>NS_ERROR_FILE_DIR_NOT_EMPTY</code></dt> <dd>Indicates that an attempt was made to remove a directory that is not empty.</dd> <dt><code>NS_ERROR_FILE_ACCESS_DENIED</code></dt> <dd>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.</dd>
</dl>
<h2 name="Remarks">Remarks</h2>
<p>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.</p>
<p>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).</p>
<p>The word "Native" appears in the name of methods that operate on or return strings encoded in the native character set.</p>
<p>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.</p>
<p>XPCOM provides the string conversion functions <a href="/zh-cn/NS_CStringToUTF16" title="zh-cn/NS_CStringToUTF16">NS_CStringToUTF16</a> and <a href="/zh-cn/NS_UTF16ToCString" title="zh-cn/NS_UTF16ToCString">NS_UTF16ToCString</a>, which can be used to convert a string between UTF-16 and the native character encoding.</p>
<p>This interface was frozen for {{ gecko("1.0") }}. See {{ Bug("129279") }} for details. From {{ gecko("2.0") }} interfaces are no longer frozen.</p>
<h2 name="See_also">See also</h2>
<ul> <li>{{ interface("nsILocalFile") }}</li> <li><a href="/zh-cn/NS_CStringToUTF16" title="zh-cn/NS_CStringToUTF16">NS_CStringToUTF16</a></li> <li><a href="/zh-cn/NS_UTF16ToCString" title="zh-cn/NS_UTF16ToCString">NS_UTF16ToCString</a></li>
</ul>
{{ languages( { "zh-cn": "zh-cn/XPCOM_Interface_Reference/nsILocalFile" } ) }}
恢复到这个版本