Filesystem
FileHandle.read()
Reads bytes into a caller-owned buffer. Use bytesRead instead of assuming every requested byte was returned. Tested on Node.js 22.16.0. Example output (one line per entry): ["4 alog"].
Formal Syntax & Structure
nodejs syntaxnodejs
handle.read(buffer, offset, length, position)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| buffer | Buffer | Destination buffer. |
| offset | number | Buffer offset. |
| length | number | Maximum byte count. |
| position | number | null | File offset or current position. |
Return Value
Promise<object>
Bytes read and buffer.
Working Example
Example Snippetnodejs
import * as fs from 'node:fs/promises';import { constants } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = await fs.mkdtemp(join(tmpdir(), 'bytemind-'));try { const p = join(dir, 'book.txt'); await fs.writeFile(p, 'catalog'); const h = await fs.open(p, 'r'); try { const b = Buffer.alloc(10); const r = await h.read(b, 0, 10, 3); console.log(r.bytesRead, b.subarray(0, r.bytesRead).toString()); } finally { await h.close(); }}finally { await fs.rm(dir, { recursive: true, force: true });}