Incremental file output
Write logs or generated content progressively without constructing the entire file in memory.
Node.js · Function
Creates and returns a writable stream connected to a file. Use it to write data incrementally while controlling file-opening behavior through options.
createWriteStream opens a file-backed writable stream. Data can be supplied incrementally with stream methods such as write() and end(); options control how the file is opened, encoded, positioned, and permissioned.
fs.createWriteStream(path[, options])| Name | Type | Description |
|---|---|---|
path | string | Buffer | URL | integer | File path to open, or a path-like value accepted by the Node.js file-system API. |
options | object | string | Optional stream and file-opening settings, including flags, encoding, fd, mode, and start. |
A new WriteStream object.
import { createWriteStream } from 'node:fs';
const output = createWriteStream('output.txt');
output.write('First line\n');
output.end('Last line\n');
output.on('finish', () => console.log('File written'));Creates a writable file stream, writes two chunks, and reports completion after the stream finishes.
Write logs or generated content progressively without constructing the entire file in memory.
Produce a file while another operation generates chunks over time.
Write to a chosen file offset or modify an existing file with suitable flags.
WriteStreamWritable Streamfs.createReadStreamfs.write