Bun

指南讀取檔案

使用 Bun 以字串形式讀取檔案

Bun.file() 函數接受路徑並傳回 BunFile 實例。BunFile 類別擴展了 Blob,並允許您以多種格式延遲讀取檔案。使用 .text() 將內容讀取為字串。

const path = "/path/to/file.txt";
const file = Bun.file(path);

const text = await file.text();
// string

任何相對路徑都將相對於專案根目錄(包含 package.json 檔案的最近目錄)解析。

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