Implements

  • FileHandle

Constructors

Properties

fd: number

The file descriptor for this file handle.

file: File

The file for this file handle

Methods

  • Asynchronously append data to a file, creating the file if it does not exist. The underlying file will not be closed automatically. The FileHandle must have been opened for appending.

    Parameters

    • data: string | Uint8Array

      The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.

    • _options: BufferEncoding | ObjectEncodingOptions & FlagAndOpenMode = {}

      Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag. If encoding is not supplied, the default of 'utf8' is used. If mode is not supplied, the default of 0o666 is used. If mode is a string, it is parsed as an octal integer. If flag is not supplied, the default of 'a' is used.

    Returns Promise<void>

  • Asynchronous fchmod(2) - Change permissions of a file.

    Parameters

    • mode: Mode

      A file mode. If a string is passed, it is parsed as an octal integer.

    Returns Promise<void>

  • Asynchronous fchown(2) - Change ownership of a file.

    Parameters

    • uid: number
    • gid: number

    Returns Promise<void>

  • Asynchronous close(2) - close a FileHandle.

    Returns Promise<void>

  • Creates a ReadStream for reading from the file.

    Parameters

    • Optional options: CreateReadStreamOptions

      Options for the readable stream

    Returns ReadStream

    A ReadStream object.

  • Creates a WriteStream for writing to the file.

    Parameters

    • Optional options: CreateWriteStreamOptions

      Options for the writeable stream.

    Returns WriteStream

    A WriteStream object

  • Asynchronous fdatasync(2) - synchronize a file's in-core state with storage device.

    Returns Promise<void>

  • Asynchronously reads data from the file. The FileHandle must have been opened for reading.

    Type Parameters

    • TBuffer extends ArrayBufferView

    Parameters

    • buffer: TBuffer

      The buffer that the data will be written to.

    • Optional offset: number

      The offset in the buffer at which to start writing.

    • Optional length: number

      The number of bytes to read.

    • Optional position: null | number

      The offset from the beginning of the file from which data should be read. If null, data will be read from the current position.

    Returns Promise<FileReadResult<TBuffer>>

  • Asynchronously reads the entire contents of a file. The underlying file will not be closed automatically. The FileHandle must have been opened for reading.

    Parameters

    • Optional _options: {
          flag?: OpenMode;
      }

      An object that may contain an optional flag. If a flag is not provided, it defaults to 'r'.

      • Optional flag?: OpenMode

    Returns Promise<Buffer>

  • Parameters

    • _options: BufferEncoding | ObjectEncodingOptions & FlagAndOpenMode

    Returns Promise<string>

  • Parameters

    • Optional options: CreateReadStreamOptions

    Returns Interface

  • Experimental

    Returns a ReadableStream that may be used to read the files data.

    An error will be thrown if this method is called more than once or is called after the FileHandle is closed or closing.

    While the ReadableStream will read the file to completion, it will not close the FileHandle automatically. User code must still call the fileHandle.close() method.

    Parameters

    • options: ReadableWebStreamOptions = {}

    Returns ReadableStream<Uint8Array>

    Since

    v17.0.0

  • Asynchronous readv. Reads into multiple buffers.

    Parameters

    • buffers: ArrayBufferView[]

      An array of Uint8Array buffers.

    • Optional position: number

      The position in the file where to begin reading.

    Returns Promise<ReadVResult>

    The number of bytes read.

  • Asynchronous fsync(2) - synchronize a file's in-core state with the underlying storage device.

    Returns Promise<void>

  • Asynchronous ftruncate(2) - Truncate a file to a specified length.

    Parameters

    • Optional len: null | number

      If not specified, defaults to 0.

    Returns Promise<void>

  • Asynchronously change file timestamps of the file.

    Parameters

    • atime: string | number | Date

      The last access time. If a string is provided, it will be coerced to number.

    • mtime: string | number | Date

      The last modified time. If a string is provided, it will be coerced to number.

    Returns Promise<void>

  • Asynchronously writes string to the file. The FileHandle must have been opened for writing. It is unsafe to call write() multiple times on the same file without waiting for the Promise to be resolved (or rejected). For this scenario, fs.createWriteStream is strongly recommended.

    Parameters

    • data: FileContents
    • Optional posOrOff: null | number
    • Optional lenOrEnc: number | BufferEncoding
    • Optional position: null | number

    Returns Promise<{
        buffer: FileContents;
        bytesWritten: number;
    }>

  • Type Parameters

    • TBuffer extends Uint8Array

    Parameters

    • buffer: TBuffer
    • Optional offset: number
    • Optional length: number
    • Optional position: number

    Returns Promise<{
        buffer: TBuffer;
        bytesWritten: number;
    }>

  • Parameters

    • data: string
    • Optional position: number
    • Optional encoding: BufferEncoding

    Returns Promise<{
        buffer: string;
        bytesWritten: number;
    }>

  • Asynchronously writes data to a file, replacing the file if it already exists. The underlying file will not be closed automatically. The FileHandle must have been opened for writing. It is unsafe to call writeFile() multiple times on the same file without waiting for the Promise to be resolved (or rejected).

    Parameters

    • data: string | Uint8Array

      The data to write. If something other than a Buffer or Uint8Array is provided, the value is coerced to a string.

    • _options: WriteFileOptions = {}

      Either the encoding for the file, or an object optionally specifying the encoding, file mode, and flag. If encoding is not supplied, the default of 'utf8' is used. If mode is not supplied, the default of 0o666 is used. If mode is a string, it is parsed as an octal integer. If flag is not supplied, the default of 'w' is used.

    Returns Promise<void>

  • Asynchronous writev. Writes from multiple buffers.

    Parameters

    • buffers: Uint8Array[]

      An array of Uint8Array buffers.

    • Optional position: number

      The position in the file where to begin writing.

    Returns Promise<WriteVResult>

    The number of bytes written.