happy-opfs
    Preparing search index...

    Function readFileSync

    Synchronous version of readFile. Reads the content of a file with the specified encoding.

    The absolute path of the file to read.

    Optional read options.

    An IOResult containing the file content.

    readFile for the async version.

    • Synchronous version of readFile. Reads the content of a file as a File object (blob encoding).

      Parameters

      • filePath: string

        The absolute path of the file to read.

      • options: ReadSyncOptions & { encoding: "blob" }

        Read options with 'blob' encoding.

      Returns IOResult<File>

      An IOResult containing a File object.

      1.1.0

      readFileSync('/path/to/file.txt', { encoding: 'blob' })
      .inspect(file => console.log(file.name, file.size));
    • Synchronous version of readFile. Reads the content of a file as a string (utf8 encoding).

      Parameters

      • filePath: string

        The absolute path of the file to read.

      • options: ReadSyncOptions & { encoding: "utf8" }

        Read options with 'utf8' encoding.

      Returns IOResult<string>

      An IOResult containing the file content as a string.

      1.1.0

      readFileSync('/path/to/file.txt', { encoding: 'utf8' })
      .inspect(content => console.log(content));
    • Synchronous version of readFile. Reads the content of a file as a Uint8Array (default).

      Parameters

      • filePath: string

        The absolute path of the file to read.

      • Optionaloptions: ReadSyncOptions & { encoding?: "bytes" }

        Optional read options. Defaults to 'bytes' encoding.

      Returns IOResult<Uint8Array<ArrayBuffer>>

      An IOResult containing the file content as a Uint8Array.

      1.1.0

      readFileSync('/path/to/file.bin')
      .inspect(bytes => console.log('First byte:', bytes[0]));
    • Synchronous version of readFile. Reads the content of a file with the specified options. This overload accepts any ReadOptions and returns the union of all possible content types. Useful when the encoding is determined at runtime.

      Parameters

      • filePath: string

        The absolute path of the file to read.

      • Optionaloptions: ReadSyncOptions

        Optional read options.

      Returns IOResult<ReadSyncFileContent>

      An IOResult containing the file content.

      readFile for the async version.

      1.1.0

      // When encoding is dynamic
      const encoding = getUserPreference(); // 'utf8' | 'bytes' | ...
      readFileSync('/path/to/file.txt', { encoding })
      .inspect(content => {
      // content type is ReadSyncFileContent (union type)
      if (typeof content === 'string') {
      console.log('Text:', content);
      } else if (content instanceof Uint8Array) {
      console.log('Bytes:', content.length);
      }
      });