Skip to content

Bun implementa il modulo node:fs, che include le funzioni fs.appendFile e fs.appendFileSync per aggiungere contenuto ai file.


Puoi usare fs.appendFile per aggiungere dati in modo asincrono a un file, creando il file se non esiste ancora. Il contenuto può essere una stringa o un Buffer.

ts
import { appendFile } from "node:fs/promises";

await appendFile("message.txt", "dati da aggiungere");

Per usare l'API non-Promise:

ts
import { appendFile } from "node:fs";

appendFile("message.txt", "dati da aggiungere", err => {
  if (err) throw err;
  console.log('I "dati da aggiungere" sono stati aggiunti al file!');
});

Per specificare la codifica del contenuto:

js
import { appendFile } from "node:fs";

appendFile("message.txt", "dati da aggiungere", "utf8", callback);

Per aggiungere i dati in modo sincrono, usa fs.appendFileSync:

ts
import { appendFileSync } from "node:fs";

appendFileSync("message.txt", "dati da aggiungere", "utf8");

Vedi la documentazione Node.js per maggiori informazioni.

Bun a cura di www.bunjs.com.cn