Bun

指南讀取檔案

使用 Bun 將檔案讀取到 ArrayBuffer

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 中使用型別化陣列的更多資訊,請參閱 型別化陣列 文件。