本文目录

Deno.seek

🌙
手机阅读
本文目录结构

seek

  • seek(rid: number, offset: number, whence: SeekMode): Promise<number>

  • 在给定查询模式 whence 和偏移量 offset 的情况下,查找指定的资源 ID(rid)。 函数将解析并返回光标在资源中的新位置(从头开始的字节数)。

    Seek a resource ID (rid) to the given offset under mode given by whence. 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)

返回 Promise<number>

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了