> ## Documentation Index
> Fetch the complete documentation index at: https://bun.manicule.dev/llms.txt
> Use this file to discover all available pages before exploring further.
> For every page in one file, fetch https://bun.manicule.dev/llms-full.txt (large).
> To search and read these docs as tools, connect the MCP server at https://bun.manicule.dev/api/mcp.
> Every agent surface: https://bun.manicule.dev/.well-known/llms-manifest.json

# Write a file incrementally (/guides/write-file/filesink)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 286 · updated: 2026-08-02 -->
Related: [Append content to a file](/guides/write-file/append.md), [Write a string to a file](/guides/write-file/basic.md), [Write a Blob to a file](/guides/write-file/blob.md), [Write a file to stdout](/guides/write-file/cat.md), [Copy a file to another location](/guides/write-file/file-cp.md), [Write a Response to a file](/guides/write-file/response.md)

Bun provides an API for incrementally writing to a file. Use it for large files, or when writing to a file over a long period of time.

Call `.writer()` on a `BunFile` to retrieve a `FileSink` instance. It buffers data; call `.flush()` to write the buffer to disk. You can write & flush many times.

```ts
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 accepts strings or binary data.

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

***

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

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

***

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

```ts
writer.end();
```

***

Full documentation: [FileSink](/runtime/file-io#incremental-writing-with-filesink).
