Skip to main content
Deno.FsFile.prototype.seekSync - Deno documentation
method Deno.FsFile.prototype.seekSync
FsFile.prototype.seekSync(
offset: number | bigint,
whence: SeekMode,
): number

Synchronously seek to the given offset under mode given by whence. The new position within the resource (bytes from the start) is returned.

using file = Deno.openSync(
  "hello.txt",
  { read: true, write: true, truncate: true, create: true },
);
file.writeSync(new TextEncoder().encode("Hello world"));

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

The seek modes work as follows:

// Given the file contains "Hello world" text, which is 11 bytes long:
using file = Deno.openSync(
  "hello.txt",
  { read: true, write: true, truncate: true, create: true },
);
file.writeSync(new TextEncoder().encode("Hello world"));

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

Parameters

offset: number | bigint
whence: SeekMode

Return Type

number