Bun.file()
函數接受路徑並傳回 BunFile
實例。BunFile
類別擴展了 Blob
,並允許您以多種格式延遲讀取檔案。
若要將檔案讀取到 Uint8Array
實例,請使用 .bytes()
檢索 BunFile
的內容。
const path = "/path/to/package.json";
const file = Bun.file(path);
const byteArray = await file.bytes();
byteArray[0]; // first byteArray
byteArray.length; // length of byteArray
請參閱API > 二進制資料 > 類型化陣列,以取得關於在 Bun 中使用 Uint8Array
和其他二進制資料格式的更多資訊。