Skip to main content
Bun.file() 函数接受一个路径并返回一个 BunFile 实例。BunFile 类继承自 Blob,允许你以多种格式延迟读取文件。使用 .arrayBuffer() 将文件读取为 ArrayBuffer
https://mintcdn.com/ikxin/RzFFGbzo0-4huILA/icons/typescript.svg?fit=max&auto=format&n=RzFFGbzo0-4huILA&q=85&s=a3dffd2241f05776d3bd25171d0c5a79index.ts
const path = "/path/to/package.json";
const file = Bun.file(path);

const buffer = await file.arrayBuffer();

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

bytes[0];
bytes.length;

请参阅 类型化数组 文档以了解在 Bun 中使用类型化数组的更多信息。