Warning: main(): unterminated entity reference _ol\]\:list-outside ol{list-style-position:outside}.\[\&_ol\]\:list-decimal ol{list-style-type:decimal}.\[\&_ol\]\:pl-4 ol{padding-left:1rem}.\[\&_ul\]\:list-outside ul{list-style-position:outside}.\[\&_ul\]\:list-disc ul{list-style-type:disc}.\[\&_ul\]\:pl-4 ul{padding-left:1rem} in /home/feelan/public_html/ir.apponweb.api/tools/agfdsjafkdsgfkyugebhekjhevbyujec.php on line 264
Write a file incrementally | Bun Examples
Bun

GuidesWriting files

Write a file incrementally with Bun

Bun provides an API for incrementally writing to a file. This is useful for writing large files, or for writing to a file over a long period of time.

Call .writer() on a BunFile to retrieve a FileSink instance. This instance can be used to efficiently buffer data and periodically "flush" it to disk. You can write & flush many times.

const file = Bun.file("/path/to/file.txt");
const writer = file.writer();

writer.write("lorem");
writer.write("ipsum");
writer.write("dolor");

writer.flush();

// continue writing & flushing

The .write() method can accept strings or binary data.

w.write("hello");
w.write(Buffer.from("there"));
w.write(new Uint8Array([0, 255, 128]));
writer.flush();

The FileSink will also auto-flush when its internal buffer is full. You can configure the buffer size with the highWaterMark option.

const file = Bun.file("/path/to/file.txt");
const writer = file.writer({ highWaterMark: 1024 * 1024 }); // 1MB

When you're done writing to the file, call .end() to auto-flush the buffer and close the file.

writer.end();

Full documentation: FileSink.