Deno.seek
seek
-
seek(rid: number, offset: number, whence: SeekMode): Promise<number>
-
在给定查询模式
whence
和偏移量offset
的情况下,查找指定的资源 ID(rid
)。 函数将解析并返回光标在资源中的新位置(从头开始的字节数)。Seek a resource ID (
rid
) to the givenoffset
under mode given bywhence
. The call resolves to the new position within the resource (bytes from the start).const file = await Deno.open('hello.txt', {read: true, write: true, truncate: true, create: true}); await Deno.write(file.rid, new TextEncoder().encode("Hello world")); // 光标前进 6 个字节 const cursorPosition = await Deno.seek(file.rid, 6, Deno.SeekMode.SEEK_START); console.log(cursorPosition); // 6 const buf = new Uint8Array(100); await file.read(buf); console.log(new TextDecoder().decode(buf)); // "world"
seek modes 的工作方式如下:
The seek modes work as follows:
// 给定内容为 "Hello world" 的 file.rid 文件,该文件长度为 11 个字节。 // 从文件开头移动 6 个字节 console.log(await Deno.seek(file.rid, 6, Deno.SeekMode.SEEK_START)); //"6" // 从当前位置再移动 2 个字节 console.log(await Deno.seek(file.rid, 2, Deno.SeekMode.SEEK_CURRENT)); //"8" // 从文件末尾向后移动 2 个字节 console.log(await Deno.seek(file.rid, -2, Deno.SeekMode.SEEK_END)); //"9" (e.g. 11-2)
参数
- ##### rid: number
- ##### offset: number
- ##### whence: [SeekMode](enums/deno.seekmode.html)