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