Bun

指南讀取檔案

使用 Bun 讀取檔案到 Uint8Array

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

有關在 Bun 中處理 Uint8Array 和其他二進位制資料格式的更多資訊,請參閱 API > 二進位制資料 > 陣列型別