使用 Deno 读取二进制文件

Read binary file with Deno

Deno 的文档似乎没有提到如何执行以下二进制文件读取操作:

  1. 从二进制文件的开头查找位置 p;
  2. 读取接下来的 4 个字节;
  3. 并将读取的内容转化为数字(32位整数);
  1. 您可以使用Deno.seek
  2. 使用file.read
  3. 使用DataView.getInt32
const file = await Deno.open('binary.file');
const p = 100; // your position
await Deno.seek(file.rid, p, Deno.SeekMode.Start);

const buf = new Uint8Array(4); // you can read more and then access a particular slice of the buffer
const bytesRead = await file.read(buf);
// ... do whatever operation you want with buf