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