Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/lib.deno.d.ts>Deno.FsFile#seek

A JavaScript extension package for building strong and modern applications.
Latest
method Deno.FsFile.prototype.seek
import { Deno } from "https://deno.land/x/ayonli_jsext@v0.9.72/lib.deno.d.ts";
const { FsFile } = Deno;

Seek to the given offset under mode given by whence. The call resolves to the new position within the resource (bytes from the start).

// Given file pointing to file with "Hello world", which is 11 bytes long:
using file = await Deno.open(
  "hello.txt",
  { read: true, write: true, truncate: true, create: true },
);
await file.write(new TextEncoder().encode("Hello world"));

// advance cursor 6 bytes
const cursorPosition = await file.seek(6, Deno.SeekMode.Start);
console.log(cursorPosition);  // 6
const buf = new Uint8Array(100);
await file.read(buf);
console.log(new TextDecoder().decode(buf)); // "world"

The seek modes work as follows:

// Given file.rid pointing to file with "Hello world", which is 11 bytes long:
const file = await Deno.open(
  "hello.txt",
  { read: true, write: true, truncate: true, create: true },
);
await file.write(new TextEncoder().encode("Hello world"));

// Seek 6 bytes from the start of the file
console.log(await file.seek(6, Deno.SeekMode.Start)); // "6"
// Seek 2 more bytes from the current position
console.log(await file.seek(2, Deno.SeekMode.Current)); // "8"
// Seek backwards 2 bytes from the end of the file
console.log(await file.seek(-2, Deno.SeekMode.End)); // "9" (i.e. 11-2)

Parameters

offset: number | bigint
whence: SeekMode

Returns

Promise<number>