skills/bun-guides-write-file-filesink/SKILL.md
Write a file incrementally
npx skillsauth add jarle/bun-skills Bun Write a file incrementallyInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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.
development
Using TypeScript with Bun, including type definitions and compiler options
development
Learn how to write tests using Bun's Jest-compatible API with support for async tests, timeouts, and various test modifiers
testing
Learn how to use snapshot testing in Bun to save and compare output between test runs
testing
Learn about Bun test's runtime integration, environment variables, timeouts, and error handling