node:fs 模块,其中包括 fs.appendFile 和 fs.appendFileSync 函数,用于向文件追加内容。
您可以使用
fs.appendFile 异步地向文件追加数据,如果文件不存在则会创建文件。内容可以是字符串或 [Buffer]。
要使用非 [Promise] API:
要指定内容的编码:
要同步追加数据,请使用
fs.appendFileSync:
有关更多信息,请参阅 Node.js 文档。
node:fs 模块,其中包括 fs.appendFile 和 fs.appendFileSync 函数,用于向文件追加内容。
fs.appendFile 异步地向文件追加数据,如果文件不存在则会创建文件。内容可以是字符串或 [Buffer]。
import { appendFile } from "node:fs/promises";
await appendFile("message.txt", "data to append");
import { appendFile } from "node:fs";
appendFile("message.txt", "data to append", err => {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
import { appendFile } from "node:fs";
appendFile("message.txt", "data to append", "utf8", callback);
fs.appendFileSync:
import { appendFileSync } from "node:fs";
appendFileSync("message.txt", "data to append", "utf8");