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 > 二进制数据 > 数组类型。