Skip to main content
Bun.file() 函数接受一个路径并返回一个 [BunFile] 实例。[BunFile] 类扩展了 Blob 并允许您以多种格式延迟读取文件。使用 .arrayBuffer() 将文件读取为 ArrayBuffer
https://mintcdn.com/teemo/2s-4Z6VdGqiCeBNX/icons/typescript.svg?fit=max&auto=format&n=2s-4Z6VdGqiCeBNX&q=85&s=087b260066909db1cd3e9c7292bc34b2index.ts
const path = "/path/to/package.json";
const file = Bun.file(path);

const buffer = await file.arrayBuffer();

ArrayBuffer 中的二进制内容随后可以作为类型化数组读取,例如 Int8Array。对于 Uint8Array,请使用 .bytes()
https://mintcdn.com/teemo/2s-4Z6VdGqiCeBNX/icons/typescript.svg?fit=max&auto=format&n=2s-4Z6VdGqiCeBNX&q=85&s=087b260066909db1cd3e9c7292bc34b2index.ts
const buffer = await file.arrayBuffer();
const bytes = new Int8Array(buffer);

bytes[0];
bytes.length;

有关在 Bun 中使用类型化数组的更多信息,请参阅 类型化数组 文档。