Bun
GitHub logo

指南写入文件

使用 Bun 将 Blob 写入文件

此代码片段将一个 Blob 对象写入磁盘上的指定路径。

它使用了快速的 Bun.write() API 来高效地将数据写入磁盘。第一个参数是目标,例如绝对路径或 BunFile 实例。第二个参数是要写入的数据

const path = "/path/to/file.txt";
await Bun.write(path, "Lorem ipsum");

BunFile 类扩展了 Blob,因此您也可以直接将 BunFile 实例传递给 Bun.write()

const path = "./out.txt";
const data = Bun.file("./in.txt");

// write the contents of ./in.txt to ./out.txt
await Bun.write(path, data);

有关 Bun.write() 的完整文档,请参阅 文档 > API > 文件 I/O