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

x/ayonli_jsext/lib.deno.d.ts>Deno.ftruncate

A JavaScript extension package for building strong and modern applications.
Latest
function Deno.ftruncate
Deprecated
Deprecated

This will be removed in Deno 2.0. See the Deno 1.x to 2.x Migration Guide for migration instructions.

import { Deno } from "https://deno.land/x/ayonli_jsext@v0.9.72/lib.deno.d.ts";
const { ftruncate } = Deno;

Truncates or extends the specified file stream, to reach the specified len.

If len is not specified then the entire file contents are truncated as if len was set to 0.

If the file previously was larger than this new length, the extra data is lost.

If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').

Truncate the entire file

const file = await Deno.open(
  "my_file.txt",
  { read: true, write: true, create: true }
);
await Deno.ftruncate(file.rid);

Truncate part of the file

const file = await Deno.open(
  "my_file.txt",
  { read: true, write: true, create: true }
);
await file.write(new TextEncoder().encode("Hello World"));
await Deno.ftruncate(file.rid, 7);
const data = new Uint8Array(32);
await Deno.read(file.rid, data);
console.log(new TextDecoder().decode(data)); // Hello W

Parameters

rid: number
optional
len: number

Returns

Promise<void>