Bun.file()
函數接受一個路徑並返回一個 BunFile
實例。BunFile
類別擴展了 Blob
,並允許您延遲讀取各種格式的檔案。使用 .arrayBuffer()
將檔案讀取為 ArrayBuffer
。
const path = "/path/to/package.json";
const file = Bun.file(path);
const buffer = await file.arrayBuffer();
ArrayBuffer
中的二進位內容可以接著讀取為類型化陣列,例如 Int8Array
。對於 Uint8Array
,請使用 .bytes()
。
const buffer = await file.arrayBuffer();
const bytes = new Int8Array(buffer);
bytes[0];
bytes.length;
請參考 類型化陣列 文件,以獲取更多關於在 Bun 中使用類型化陣列的資訊。