Bun

指南寫入檔案

使用 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);

請參閱 文件 > API > 檔案 I/O 以取得 Bun.write() 的完整文件。