Skip to main content

Write a ReadableStream to a file

还没翻译译自英文(bun.com)本页 4 段都还没译

To write a ReadableStream to disk, call .writer() on a BunFile to get a 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.

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 does not truncate an existing file. If the file may already exist, delete it first.


See FileSink.

翻訳 © 2026 fanyi.dev · 原文 © 2026 the Bun project (LICENSE.md names no copyright holder) (MIT)