![]() |
v1.3.0.0
|
The File class provides access to... files!
It has many static functions which are accessed in JS with the File. qualifier. These are "atomic" operations, eg. read a whole file at once or check if a file name exists, etc.
File operations such as reading, writing, or copying have synchronous and asynchronous versions. The async versions can return a Promise or invoke a callback function to return results (see the individual methods for details). Async operations run in a separate thread taken from a shared pool.
As of DSE v1.3, the synchronous versions of copy(), read(), remove(), and rename() methods will throw exceptions for all errors. Prior to v1.3, exceptions were only thrown when a file couldn't be opened for read() but otherwise would simply return a status indicator result (eg. true/false). To disable exceptions and revert to the old behavior, set the throwExceptions property to false.
/ directory separator.Member Objects | |
| struct | Options |
Properties | |
| bool | throwExceptions |
Static Public Member Functions | |
File Information | |
| FileInfo | info (string &path) |
| bool | exists (string &file) |
| bool | isFile (string &path) |
| bool | isDir (string &path) |
| bool | isReadable (string &file) |
| bool | isWritable (string &file) |
| bool | isAbs (string &file) |
| bool | isExec (string &file) |
| uint | size (string &file) |
| FS::Permissions | permissions (string &file) |
| bool | setPermissions (string &file, FS::Permissions p) |
Path Information | |
| string | absPath (string &path) |
| string | absFilePath (string &file) |
| string | baseName (string &file) |
| string | filePath (string &file) |
| string | fullBaseName (string &file) |
| string | fullSuffix (string &file) |
| string | name (string &file) |
| string | normPath (string &path) |
| string | normFilePath (string &file) |
| string | path (string &file) |
| string | suffix (string &file) |
Timestamps | |
| Date | atime (string &file) |
| Date | btime (string &file) |
| Date | ctime (string &file) |
| Date | mtime (string &file) |
File Actions | |
| bool | copy (string from, string to, FS.OverwriteMode overwriteMode=FS.OW_EXCL) |
| < Promise|void > | copyAsync (string from, string to,< FS.OverwriteMode|Function > mode_or_callback,< Function|undefined > callback) |
| bool | link (string fileName, string linkName) |
| < ArrayBuffer|String > | read (string file,< Options|string|FS.OpenMode|undefined > options) |
| < Promise|void > | readAsync (string file,< Options|string|FS.OpenMode|Function|undefined > options,< Function|undefined > callback) |
| string | readLines (string file, int maxLines, int fromLine=0, bool trimTrailingNewlines=true) |
| < Promise|void > | readLinesAsync (string file, int maxLines,< int|undefined > fromLine=0,< boolean|undefined > trimTrailingNewlines,< Function|undefined > callback) |
| string | readText (string file) |
| bool | remove (string file) |
| < Promise|void > | removeAsync (string file,< Function|undefined > callback) |
| bool | rename (string oldName, string destination, FS.OverwriteMode overwriteMode=FS.OW_EXCL) |
| < Promise|void > | renameAsync (string oldName, string destination,< FS.OverwriteMode|Function > mode_or_callback,< Function|undefined > callback) |
| int | write (string file,< string|ArrayBuffer > data,< Options|string|FS.OpenMode|undefined > options) |
| < Promise< int >|void > | writeAsync (string file,< string|ArrayBuffer > data,< Options|string|FS.OpenMode|Function|undefined > options,< Function|undefined > callback) |
| struct File::Options |
Options object which can be passed as an argument to various file reading and writing methods. All properties are optional.
| Class Members | ||
|---|---|---|
| int | chunkSize |
Some file operations, like read()/readAsync() and write()/writeAsync(), will operate on "byte chunks" of a file at a time. By default the system tries to determine an optimal chunk size (the block size of the partition being accessed, eg. typically 4KB on an NTFS volume), but can be overridden with this setting. This may especially be relevant if wanting to abort an operatin in progress (with This value is set in bytes. |
| string | encoding |
Specify the name of a text encoding type to use for reading or writing text files. Specifying an encoding when reading files will always return the results as a UTF-8 string (even if the file Note that text files in UTF-8 and compatible formats generally don't require any special handling, the default behavior when reading/writing in text mode is usually sufficient. Supported encoding types (dashes, spaces and case in names are ignored, eg.
|
| < FS.OpenMode|string > | mode |
A string or enumeration corresponding to the FS.OpenMode flag value(s) to use when opening files. For most file operations the default is usually FS.O_TEXT. |
| AbortSignal | signal |
An AbortSignal obtained from an AbortController may be used to cancel long-running asynchronous file operations. Note that cancellation happens on a "best efforts" basis since we cannot interrupt the system-level operations (see |
| int | readMaxSize |
When reading files, sets the maximum number of bytes to read. Default is to read the whole file. Ignored when writing files. |
| boolean | writeBOM |
When writing text files with a UTF-type encoding specified, this controls if a byte order mark is generated in the output. If not specified, by default a BOM is written for UTF-16 and UTF-32 encodings, but not for UTF-8. To add a BOM to UTF-8 text files, set this option to Ignored when reading files or when an |
| boolean | writeCR |
When writing text files with an encoding specified, this sets if the line endings should include the Carriage Return character (\r\n) or not (\n). By default this is Ignored when reading files or when an |
| < boolean|string > | writeSafe |
When writing files, this option can enable using a temporary intermediate file as a fallback mechanism. When enabled, the data is written to a temporary file first. If that completes without errors, then the temporary file is moved to the actual requested location (possibly overwriting any existing file). If writing the temporary file fails for any reason, it is automatically removed afterwards. This ensures that no data in the original file is lost in case an error happens while writing, and no partially-written file is ever present at the final location. However, when overwriting existing files, this requires that This option can have one of 3 values:
Ignored when reading files. |
|
readwrite |
The throwExceptions global property controls if exceptions are thrown for synchronous file operations copy(), read(), remove(), rename(), and write().
If set to true (the default), these functions will throw exceptions when any error is encountered (either with argument validation or from the file system itself).
If set to false then these functions will not throw but instead return some value indicating failure, as per their documentation (eg. false or -1 for bytes written with write()). There is no way to programmatically determine the nature of the error this way. However, the generated error message will instead be written to the plugin's log files (plugin.log and console.log).
File operations for the whole script engine environment/instance.
|
static |
Returns a FileInfo object describing the file or directory at the given path. This function is equivalent to Dir.info(). If you wish to access multiple attributes about the same file, this is more efficient than separately calling individual File status functions on the same file (like File.size(file) then File.isReadable(file) followed by File.isWritable(file), for example). Relative paths are resolved against the current working directory (Dir.cwd()).
|
static |
Returns true if file exists; otherwise returns false.
|
static |
Returns true if path points to a file or to a symbolic link to a file. Returns false if the object points to something which isn't a file, such as a directory.
|
static |
Returns true if path points to a directory or to a symbolic link to a directory; otherwise returns false.
|
static |
Returns true if the user can read the file file; otherwise returns false.
|
static |
Returns true if the user can write to the file file; otherwise returns false.
|
static |
Returns true if the file path name is absolute, otherwise returns false if the path is relative.
|
static |
Returns true if the file is executable; otherwise returns false.
|
static |
Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.
|
static |
Returns the complete OR-ed together combination of FS.Permissions for the file.
|
static |
Sets the permissions for file to the FS.Permissions flags specified in p. Returns true if successful, or false if the permissions cannot be modified.
|
static |
Returns the file's absolute path, excluding the file name.
|
static |
Returns the file's absolute path, including the file name (with extension).
|
static |
Returns the base name of the file without the path. The base name consists of all characters in the file up to (but not including) the first '.' character.
|
static |
Returns the file name, including the path (which may be absolute or relative).
|
static |
Returns the complete base name of the file without the path. The full base name consists of all characters in the file up to (but not including) the last '.' character.
|
static |
Returns the "full" suffix (extension) of the file. The full suffix consists of all characters in the file after (but not including) the first '.'.
|
static |
Returns the name of a file (with suffix), excluding the path.
|
static |
Returns the file's path canonical path (excluding the file name), i.e. an absolute path without symbolic links or redundant "." or ".." elements.
|
static |
Returns the canonical path including the file name, i.e. an absolute path without symbolic links or redundant "." or ".." elements.
|
static |
Returns the file's path, excluding the file name.
|
static |
Returns the suffix (extension) of the file. The suffix consists of all characters in the file after (but not including) the last '.'.
|
static |
Returns the date and local time when the file was last accessed (read). If the file is not available, this function returns an invalid Date object.
|
static |
Returns the date and time when the file was created / born. If the file birth time is not available, this function returns an invalid Date object.
|
static |
Returns the date and time when the file metadata (status, eg. permissions) was changed. If the file is not available, this function returns an invalid Date object.
|
static |
Returns the date and local time when the file was last modified. If the file is not available, this function returns an invalid Date object.
|
static |
Synchronously copies the from file to a file or directory specified in to.
Returns true if successful; otherwise returns false or throws an exception if throwExceptions is true (the default).
If to is a directory, the file name part of from is automatically appended to it. For a name to be recognized as a directory it must already exist in the file system or end in a slash (/). Otherwise it is interpreted as a file name.
Destination directory tree must already exist or an error is thrown.
If a file with the name to already exists, behavior depends on the value of overwriteMode argument. By default existing files are not overwritten and an exception will be generated.
Setting overwriteMode to one of the other FS.OverwriteMode values will instead attempt to overwrite the existing file. See enumeration documentation for options.
If the copied file is a symbolic link (symlink), the file it refers to is copied, not the link itself. With the exception of permissions, which are copied, no other file metadata is copied.
If copy fails before it is complete (for example due to a full disk partition), any partially written file is removed.
| TypeError | if argument validation fails. |
| DOMException.NotFoundError | if source file doesn't exist. |
| DOMException.InvalidAccessError | if destination file already exists and overwriteMode is FS.OW_EXCL or if a safe overwrite failed (eg. could not rename old destination file). |
| DOMException.OperationError | if any other file system error occurred during the copy operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Asynchronously copies the from file to a file or directory specified in to.
Results of the write operation are available either as a Promise or by providing a callback function.
This method has multiple possible signatures. Each version takes an optional callback Function as the last argument.
copyAsync() will return a Promise which resolves with a boolean value of true, or rejects with an Error argument.true or an Error) and this method returns undefined.callback(error, success)error - An Error object if any exception occurred while trying to copy the file. See below for more details on returned error types. If there was no error this argument value will be null.success - boolean value of true if copy was successful, or null if there was an error.Available method signatures:
copyAsync(string from, string to [, Function callback])from to to with FS.OW_EXCL overwrite mode (copy fails if destination file exists).copyAsync(string from, string to, FS.OverwriteMode overwriteMode [, Function callback])from to to using overwriteMode value to determine what to do in case destination already exists.If to is a directory, the file name part of from is automatically appended to it. For a name to be recognized as a directory it must already exist in the file system or end in a slash (/). Otherwise it is interpreted as a file name.
Destination directory tree must already exist or an error occurs.
If a file with the name to already exists, behavior depends on the value of overwriteMode argument. By default existing files are not overwritten and an exception will be generated.
Setting overwriteMode to one of the other FS.OverwriteMode values will instead attempt to overwrite the existing file. See enumeration documentation for options.
If the copied file is a symbolic link (symlink), the file it refers to is copied, not the link itself. With the exception of permissions, which are copied, no other file metadata is copied.
If copy fails before it is complete (for example due to a full disk partition), any partially written file is removed.
All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError if argument validation fails.DOMException.NotFoundError if source file doesn't exist.DOMException.InvalidAccessError if destination file already exists and overwriteMode is FS.OW_EXCL or if a safe overwrite failed (eg. could not rename old destination file).DOMException.OperationError if any other file system error occurred during the copy operation.
|
static |
Creates a link named linkName that points to the file fileName.
What a link is depends on the underlying filesystem (be it a shortcut on Windows or a symbolic link on Unix). Returns true if successful; otherwise returns false.
|
static |
Synchronously reads the contents of a file and returns them as a String or ArrayBuffer.
If reading fails, returns an empty string/array or throws an exception if throwExceptions is true (the default).
Available method signatures and their return types:
read(string file) => ArrayBufferfile in text mode (FS.O_TEXT) and returns the contents as a raw ArrayBuffer of bytes read, with no decoding.read(string file, <string | FS.OpenMode> mode) => ArrayBufferfile using specified FS.OpenMode mode flag(s) (which can be a string or enumeration values) and returns the contents as an ArrayBuffer of bytes read.read(string file, string encoding) => stringfile as text and returns it as a String after decoding the contents using the specified encoding.read(string file, Options options) => <ArrayBuffer | string>file using settings specified in options object. Returns data as a decoded String if an encoding type was specified in options, otherwise as a raw ArrayBuffer of bytes.See the shared Options documentation for more details on supported options and encoding types.
| TypeError | if argument validation fails. |
| DOMException.NotFoundError | if source file doesn't exist. |
| DOMException.OperationError | for other errors when trying to open a file (like lack of permissions). |
| DOMException.InvalidStateError | for file system errors during the actual read operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Read the contents of a file asynchronously using a callback or Promise to handle the results.
This method has multiple possible signatures. Each version takes an optional callback Function as the last argument.
readAsync() will return a Promise which resolves with the contents of the file (an ArrayBuffer or String, depending on options), or rejects with an Error argument.Error) and this method returns undefined.callback(error, data)error - An Error object if any exception occurred while trying to read the file. See below for more details on returned error types. If there was no error this argument value will be null.data - The contents of the file if no error occurred (an ArrayBuffer or String, depending on options), or null if there was an error.Available method signatures and their possible return types:
readAsync(string file [, Function callback]) => <Promise<ArrayBuffer> | void> file in text mode (FS.O_TEXT) and returns the contents as a raw ArrayBuffer of bytes read, with no decoding.readAsync(string file, <string | FS.OpenMode> mode [, Function callback]) => < Promise<ArrayBuffer> | void>file using specified FS.OpenMode mode flag(s) (which can be a string or enumeration values) and returns the contents as an ArrayBuffer of bytes read.readAsync(string file, string encoding [, Function callback]) => <Promise<string> | void>file as text and returns it as a String after decoding the contents using the specified encoding.readAsync(string file, object options [, Function callback]) => <Promise< ArrayBuffer | string > | void>file using settings specified in options object. Returns data as a decoded String if an encoding type was specified in options, otherwise as a raw ArrayBuffer of bytes.See the shared Options documentation for more details on supported options and encoding types.
All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError types.DOMException.NotFoundError if source file doesn't exist.DOMException.OperationError.DOMException.InvalidStateError.AbortSignal (passed on options argument) then a DOMException.AbortError is generated.
|
static |
Reads up to maxLines lines from file starting at fromLine (default = 0, start of file) and returns the contents as a string.
Can search from start or end of file.
| file | The file to read lines from. Relative paths are resolved from the plugin's working directory – absolute paths are recommended. |
| maxLines | Required. Can be 0 in which case all remaining lines in the file are returned. This is useful when fromLine != 0 to skip a number of lines and then return the rest. |
| fromLine | Which line to start from. Optional, default is 0 (zero). This Can be negative, in which case the file will be read from the end, backwards. In this case -1 means starting at the end of the file, -2 means to skip one line ("start at second-to-last line"), and so on. |
| trimTrailingNewlines | Optional. If true (default) trims/skips over any empty lines from the end of the file. When searching backwards, setting trimTrailingNewlines to false will count every newline from the end, including any potential newline at end of the last line of the file. |
This operation works on all text files regardless of line endings. The file encoding is determined automatically, although for UTF-16 and UTF-32 formats this requires the files to have a leading byte order mark (BOM) header.
| TypeError | if argument validation fails. |
| DOMException.NotFoundError | if source file doesn't exist. |
| DOMException.OperationError | for other errors when trying to open a file (like lack of permissions). |
| DOMException.InvalidStateError | for file system errors during the actual read operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Asynchronously Reads up to maxLines lines from file starting at fromLine (default = 0, start of file) and returns the contents as a string using a callback or Promise to handle the results.
Can search from start or end of file.
| file | The file to read lines from. Relative paths are resolved from the plugin's working directory – absolute paths are recommended. |
| maxLines | Required. Can be 0 in which case all remaining lines in the file are returned. This is useful when fromLine != 0 to skip a number of lines and then return the rest. |
| fromLine | Which line to start from. Optional, default is 0 (zero). This Can be negative, in which case the file will be read from the end, backwards. In this case -1 means starting at the end of the file, -2 means to skip one line ("start at second-to-last line"), and so on. |
| trimTrailingNewlines | Optional. If true (default) trims/skips over any empty lines from the end of the file. When searching backwards, setting trimTrailingNewlines to false will count every newline from the end, including any potential newline at end of the last line of the file. |
| callback | Optional.
|
This operation works on all text files regardless of line endings. The file encoding is determined automatically, although for UTF-16 and UTF-32 formats this requires the files to have a leading byte order mark (BOM) header.
All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError types.DOMException.NotFoundError if source file doesn't exist.DOMException.OperationError.DOMException.InvalidStateError.
|
static |
Reads a file in text mode and returns the contents as a string.
This is a convenience method which is equivalent to calling String(File.read(file));
| ReferenceError | is thrown if file loading fails (file not found/etc) and returns and empty string. |
|
static |
Synchronously deletes the file specified by file.
Returns true if successful; otherwise returns false or throws an exception if throwExceptions is true (the default).
| TypeError | if argument validation fails (file argument is empty). |
| DOMException.NotFoundError | if file doesn't exist. |
| DOMException.OperationError | if any other file system error occurred during the delete operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Asynchronously deletes the file specified by file.
Results of the deletion operation are available either as a Promise or by providing a callback function.
callback argument is given, removeAsync() will return a Promise which resolves with a boolean value of true, or rejects with an Error argument.true or an Error) and this method returns undefined.callback(error, success)error - An Error object if any exception occurred while trying to delete the file. See below for more details on returned error types. If there was no error this argument value will be null.success - boolean value of true if rename was successful, or null if there was an error.All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError if argument validation fails.DOMException.NotFoundError if source file doesn't exist.DOMException.OperationError if any other file system error occurred during the copy operation.
|
static |
Synchronously renames (or moves) the oldName file to a file or directory specified in destination.
Returns true if successful; otherwise returns false or throws an exception if throwExceptions is true (the default).
If destination is a directory, the file name part of oldName is automatically appended to it. For a name to be recognized as a directory it must already exist in the file system or end in a slash (/). Otherwise it is interpreted as a file name.
Destination directory tree must already exist or an error is thrown.
If a file with the name destination already exists, behavior depends on the value of overwriteMode argument. By default existing files are not overwritten and an exception will be generated.
Setting overwriteMode to one of the other FS.OverwriteMode values will instead attempt to overwrite the existing file. See enumeration documentation for options.
If the rename operation fails initially, this will attempt to copy oldName file's contents to destination, and then remove the oldName file, keeping only destination. If that copy operation fails or the oldName file can't be removed, the destination file destination is removed to restore the old state.
| TypeError | if argument validation fails. |
| DOMException.NotFoundError | if either the source file or destination directory don't exist. |
| DOMException.InvalidAccessError | if destination file already exists and overwriteMode is FS.OW_EXCL or if a safe overwrite failed (eg. could not rename old destination file). |
| DOMException.OperationError | if any other file system error occurred during the copy operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Asynchronously renames (or moves) the oldName file to a file or directory specified in destination.
Results of the write operation are available either as a Promise or by providing a callback function.
This method has multiple possible signatures. Each version takes an optional callback Function as the last argument.
renameAsync() will return a Promise which resolves with a boolean value of true, or rejects with an Error argument.true or an Error) and this method returns undefined.callback(error, success)error - An Error object if any exception occurred while trying to copy the file. See below for more details on returned error types. If there was no error this argument value will be null.success - boolean value of true if rename was successful, or null if there was an error.Available method signatures:
renameAsync(string oldName, string destination [, Function callback])oldName to destination with FS.OW_EXCL overwrite mode (fails if destination file exists).renameAsync(string oldName, string destination, FS.OverwriteMode overwriteMode [, Function callback])oldName to destination using overwriteMode value to determine what to do in case destination already exists.If destination is a directory, the file name part of oldName is automatically appended to it. For a name to be recognized as a directory it must already exist in the file system or end in a slash (/). Otherwise it is interpreted as a file name.
Destination directory tree must already exist or an error occurs.
If a file with the name destination already exists, behavior depends on the value of overwriteMode argument. By default existing files are not overwritten and an exception will be generated.
Setting overwriteMode to one of the other FS.OverwriteMode values will instead attempt to overwrite the existing file. See enumeration documentation for options.
If the rename operation fails initially, this will attempt to copy oldName file's contents to destination, and then remove the oldName file, keeping only destination. If that copy operation fails or the oldName file can't be removed, the destination file is removed to restore the old state.
All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError if argument validation fails.DOMException.NotFoundError if source file doesn't exist.DOMException.InvalidAccessError if destination file already exists and overwriteMode is FS.OW_EXCL or if a safe overwrite failed (eg. could not rename old destination file).DOMException.OperationError if any other file system error occurred during the copy operation.
|
static |
Synchronously writes data to file and returns the number of bytes written.
Returns -1 on error or throws an exception if throwExceptions is true (the default).
Available method signatures:
write(string file, <string | ArrayBuffer> data)data to file in text mode (FS.O_TEXT). No specific text encoding is applied to data, but on Windows line endings may be converted to \r\n.write(string file, <string | ArrayBuffer> data, <string | FS.OpenMode> mode)data to file using specified FS.OpenMode mode flag(s) when opening/creating the file (which can be a string or enumeration values).write(string file, <string | ArrayBuffer> data, string encoding)data to file after encoding it as text using the specified encoding type.write(string file, <string | ArrayBuffer> data, Options options)data to file using settings specified in options object.See the shared Options documentation for more details on supported options and encoding types.
By default file is overwritten with the contents of data (otherwise known as "truncating" the file). To append to an existing file instead, set the FS.O_APPEND (or "a") file mode flag. To return an error if the file already exists, set the FS.O_EXCL ("x") flag. Rr, conversely, to only write to existing files use the FS.O_NOCREAT ("n") flag.
Note that if writing UTF text with a byte order mark, the BOM size will be included in the resulting count of bytes written (3 bytes for UTF-8, 2 bytes for UTF-16, and 4 bytes for UTF-32).
| TypeError | if argument validation fails. |
| DOMException.OperationError | when trying to open a file (like lack of permissions, bad file path, etc). |
| DOMException.InvalidStateError | for file system errors during the actual write operation. |
Exceptions are only thrown if the current value of throwExceptions is true (the default).
|
static |
Asynchronously writes data to file.
Results of the write operation are available either as a Promise or by providing a callback function.
This method has multiple possible signatures. Each version takes an optional callback Function as the last argument.
writeAsync() will return a Promise which resolves with the number of bytes written, or rejects with an Error argument.Error) and this method returns undefined.callback(error, bytesWritten)error - An Error object if any exception occurred while trying to read the file. See below for more details on returned error types. If there was no error this argument value will be null.bytesWritten - The number of bytes written to file, or null if there was an error.Available method signatures:
write(string file, <string | ArrayBuffer> data [, Function callback])data to file in text mode (FS.O_TEXT). No specific text encoding is applied to data, but on Windows line endings may be converted to \r\n.write(string file, <string | ArrayBuffer> data, <string | FS.OpenMode> mode [, Function callback])data to file using specified FS.OpenMode mode flag(s) when opening/creating the file (which can be a string or enumeration values).write(string file, <string | ArrayBuffer> data, string encoding [, Function callback])data to file after encoding it as text using the specified encoding type.write(string file, <string | ArrayBuffer> data, Options options [, Function callback])data to file using settings specified in options object.See the shared Options documentation for more details on supported options and encoding types.
By default file is overwritten with the contents of data (otherwise known as "truncating" the file). To append to an existing file instead, set the FS.O_APPEND (or "a") file mode flag. To return an error if the file already exists, set the FS.O_EXCL ("x") flag. Rr, conversely, to only write to existing files use the FS.O_NOCREAT ("n") flag.
Note that if writing UTF text with a byte order mark, the BOM size will be included in the resulting count of bytes written (3 bytes for UTF-8, 2 bytes for UTF-16, and 4 bytes for UTF-32).
All errors and exceptions are reported asynchronously, via either a rejected Promise or in the error argument passed to the callback. This method doesn't throw exceptions.
TypeError types.DOMException.OperationError.DOMException.InvalidStateError.AbortSignal (passed on options argument) then a DOMException.AbortError is generated.