Constructor
new File(path)
Creates a new File object.
Parameters:
Name | Type | Description |
---|---|---|
path |
string | path to the file |
Methods
(async) append(contents, optionsopt)
Append contents to the file.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
contents |
string | Buffer | contents to be written to the file |
||
options |
object |
<optional> |
{}
|
contains options for appending to the file The options can include parameters such as fileMode, dirMode, retries and encoding. |
(async) atime() → {Date}
Returns a Date object representing the time when file was last accessed.
Returns:
Date object, if file exists and its stats are read successfully; 0, otherwise
- Type
- Date
(async) chmod(mode) → {number}
Change the mode of the file.
Parameters:
Name | Type | Description |
---|---|---|
mode |
number | string | An octal number or a string representing the file mode |
Returns:
0, on success; -1, if some error occurred
- Type
- number
(async) chmodr(mode) → {number}
Change the mode of the file or directory recursively.
Parameters:
Name | Type | Description |
---|---|---|
mode |
number | string | An octal number or a string representing the file mode |
Returns:
0, on success; -1, if some error occurred
- Type
- number
(async) chown(user, group) → {number}
Change the owner and group of the file.
Parameters:
Name | Type | Description |
---|---|---|
user |
number | string | user id, or user name |
group |
number | string | group id, or group name |
Returns:
0, on success; any other number, if some error occurred
NOTE: If the owner or group is specified as -1, then that ID is not changed
- Type
- number
(async) chownr(user, group) → {number}
Change the owner and group of the file recursively.
Parameters:
Name | Type | Description |
---|---|---|
user |
number | string | user id, or user name |
group |
number | string | group id, or group name |
Returns:
0, on success; any other number, if some error occurred
NOTE: If the owner or group is specified as -1, then that ID is not changed
- Type
- number
(async) copy(destination, optionsopt)
Copy the file to some destination.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
destination |
string | path of the destination |
||
options |
object |
<optional> |
{}
|
options for copying the file If the overwrite option is explicitly set to false, only then |
(async) crtime() → {Date}
Returns a Date object representing the time when file was created.
Returns:
Date object, if file exists and its stats are read successfully; 0, otherwise
- Type
- Date
(async) ctime() → {Date}
Returns a Date object representing the time when file was last changed.
Returns:
Date object, if file exists and its stats are read successfully; 0, otherwise
- Type
- Date
(async) exists() → {boolean}
Checks whether a file exists already.
Returns:
true, if the file exists; false, otherwise
- Type
- boolean
existsSync() → {boolean}
Checks whether a file exists already.
Returns:
true, if the file exists; false, otherwise
- Type
- boolean
(async) glob() → {Array.<string>}
Perform a glob search with the path of the file as the pattern.
Returns:
Array containing the matches
- Type
- Array.<string>
(async) isDir() → {boolean}
Returns whether this File object represents a directory.
Returns:
true, if this object represents a directory; false, otherwise
- Type
- boolean
(async) isFile() → {boolean}
Returns whether this File object represents a file.
Returns:
true, if this object represents a file; false, otherwise
- Type
- boolean
(async) lstat() → {object}
Returns an object with the stats of the file. If the path for the file
is a symlink, then stats of the symlink are returned.
Returns:
Stats object
- Type
- object
(async) mkdir(modeopt)
Create a directory.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mode |
number |
<optional> |
0o755
|
file mode for the directory |
(async) mkdirp(modeopt)
Create a new directory and any necessary subdirectories.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mode |
number |
<optional> |
0o755
|
file mode for the directory |
(async) mkdirpPath(modeopt)
Create (all necessary directories for) the path of the file/directory.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
mode |
number |
<optional> |
0o755
|
file mode for the directory |
(async) mtime() → {Date}
Returns a Date object representing the time when file was last modified.
Returns:
Date object, if file exists and its stats are read successfully; 0, otherwise
- Type
- Date
(async) mv(newName) → {number}
Move file to a new location
Parameters:
Name | Type | Description |
---|---|---|
newName |
string | new location (or path) for the file |
Returns:
0, on success; -1, if some error occurred
- Type
- number
(async) read() → {string|Buffer}
Read contents of the file.
Returns:
contents of the file
- Type
- string | Buffer
(async) realpath() → {string}
Return the canonicalized absolute pathname
Returns:
the resolved path
- Type
- string
realpathSync() → {string}
Return the canonicalized absolute pathname
Returns:
the resolved path
- Type
- string
(async) rename(newName) → {number}
Change the name or location of the file.
Parameters:
Name | Type | Description |
---|---|---|
newName |
string | new path/location (not just name) for the file |
Returns:
0, on success; -1, if some error occurred
- Type
- number
(async) rm()
Remove the file.
NOTE: The path is unlinked from the file, but the file
is deleted only if the path was the only link to the file and
the file was not opened in any other process.
(async) rmdir()
Remove the directory.
NOTE: The directory will be deleted only if it is empty.
(async) rmrf()
Recursively delete the directory and all its contents.
(async) size() → {number}
Returns the size of the file in bytes. If the file is not found
or can't be read successfully, 0 is returned.
Returns:
Size of file (in bytes)
- Type
- number
(async) stat() → {object}
Returns an object with the stats of the file. If the path for the file
is a symlink, then stats of the target of the symlink are returned.
Returns:
Stats object
- Type
- object
(async) unlink()
Unlink the path from the file.
NOTE: If the path referred to a
symbolic link, the link is removed. If the path is the only link
to the file then the file will be deleted.
(async) write(contents, optionsopt)
Write contents to the file.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
contents |
string | Buffer | contents to be written to the file |
||
options |
object |
<optional> |
{}
|
contains options for writing to the file |