Skip to content

Bun.file() 函數接受一個路徑並返回一個 BunFile 實例。BunFile 類擴展了 Blob,允許你以多種格式延遲讀取文件。使用 .arrayBuffer() 將文件讀取為 ArrayBuffer

ts
const path = "/path/to/package.json";
const file = Bun.file(path);

const buffer = await file.arrayBuffer();

然後可以將 ArrayBuffer 中的二進制內容讀取為類型化數組,例如 Int8Array。對於 Uint8Array,請使用 .bytes()

ts
const buffer = await file.arrayBuffer();
const bytes = new Int8Array(buffer);

bytes[0];
bytes.length;

請參閱 類型化數組 文檔,了解在 Bun 中使用類型化數組的更多信息。

Bun學習網由www.bunjs.com.cn整理維護