V8 Javascript API

fs.async.File_handle Class Reference

[File System]

Description

A File_handle object is a wrapper for a File_descriptor for use in the asynchronous fs.async api. It provides an asynchronous object oriented API for working with files.

Note:
  • Like all other asynchronous functionality any command using this class must return a Promise.

  • Instances of the File_handle object are created by the fs.async.open() method.

Functions

Promise close()
Closes an open file handle. More...
Promise read( Uint8Array data, Integer offset, Integer length, Integer position)
Reads data from the file. More...
Promise write( ArrayBuffer data, Integer offset, Integer length, Integer position)
Writes data to the file. More...
Promise write( undefined data, Integer position)
Writes a string to the file. More...

Functions

Promise fs.​async.​File_handle.close()

Closes an open file handle.

Returns

A promise that resolves when the file is closed.

Promise fs.​async.​File_handle.read( Uint8Array data, Integer offset, Integer length, Integer position)

Reads data from the file.

Parameters

data
The data to read into. May be one of Uint8Array or Binary
offset
offset into data to start writing at. Optional, defaults to 0.
length
the number of bytes to read. Required if data is an empty Binary. Otherwise optional, and defaults to the data length - offset.
position
the position in the file to start reading from, if not a number, or < 0 then reads from the current position.

Returns

The promise resolves with an object with a bytesRead property specifying the number of bytes read, and a data property that is a reference to the passed in data argument. The promise rejects on error.

Promise fs.​async.​File_handle.write( ArrayBuffer data, Integer offset, Integer length, Integer position)

Writes data to the file.

Parameters

data
The data to write to disk. May be one of the following:
  • ArrayBuffer
  • Uint8Array
  • Binary
offset
into the start of data to start writing from. Optional, defaults to 0.
length
the number of bytes in data to write. Optional, defaults to the entire buffer.
position
the position in the file to start writing at, if not a number, or < 0 then writes at current position.

Returns

The promise resolves with an object with a bytesWritten property specifying the number of bytes written, and a data property that is a reference to the passed in data argument. The promise rejects on error. It is unsafe to use File_handle.write() multiple times on the same file without waiting for the Promise to be resolved (or rejected)

Promise fs.​async.​File_handle.write( undefined data, Integer position)

Writes a string to the file.

Note:

Unlike when writing a buffer, the entire string must be written. No substring may be specified. This is because the byte offset of the resulting data may not be the same as the string offset.

Note:

Strings are always written using utf8 encoding.

Parameters

data
position
the position in the file to start writing at, if not a number, or < 0 then writes at current position.

Returns

The promise resolves with an object with a bytesWritten property specifying the number of bytes written, and a data property that is a reference to the passed in data argument. The promise rejects on error.