> ## Documentation Index
> Fetch the complete documentation index at: https://bun-1dd33a4e-farm-de84d354-pm-sbom.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Write a ReadableStream to a file

To write a `ReadableStream` to disk, call `.writer()` on a `BunFile` to get a [`FileSink`](/runtime/file-io#incremental-writing-with-filesink). The stream is an async iterable, so write each of its chunks to the `FileSink` with `for await`, then call `.end()` to flush the buffer and close the file.

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
const stream: ReadableStream = ...;
const path = "./file.txt";
const writer = Bun.file(path).writer();

for await (const chunk of stream) {
  writer.write(chunk);
}

await writer.end();
```

***

`.writer()` creates the file if it doesn't exist, but it does not truncate an existing file. If the file may already exist, delete it first.

***

See [`FileSink`](/runtime/file-io#incremental-writing-with-filesink).
