Bun.file()
函式接受路徑並傳回 BunFile
執行個體。BunFile
類別擴充 Blob
,並允許您以多種格式延遲讀取檔案。使用 .arrayBuffer()
將檔案讀取為 ArrayBuffer
。
const path = "/path/to/package.json";
const file = Bun.file(path);
const buffer = await file.arrayBuffer();
然後可以將 ArrayBuffer
中的二進位內容讀取為類型化陣列,例如 Uint8Array
。
const buffer = await file.arrayBuffer();
const bytes = new Uint8Array(buffer);
bytes[0];
bytes.length;
參閱 類型化陣列 文件,以取得在 Bun 中使用類型化陣列的更多資訊。