> ## Documentation Index
> Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 追加内容到文件

Bun 实现了 `node:fs` 模块，其中包括用于向文件追加内容的 `fs.appendFile` 和 `fs.appendFileSync` 函数。

***

你可以使用 `fs.appendFile` 异步地向文件追加数据，如果文件不存在则创建该文件。内容可以是字符串或 `Buffer`。

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
import { appendFile } from "node:fs/promises";

await appendFile("message.txt", "data to append");
```

***

若要使用非 Promise API：

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
import { appendFile } from "node:fs";

appendFile("message.txt", "data to append", err => {
  if (err) throw err;
  console.log('已将 "data to append" 追加到文件！');
});
```

***

若要指定内容的编码：

```js theme={"theme":{"light":"github-light","dark":"dracula"}}
import { appendFile } from "node:fs";

appendFile("message.txt", "data to append", "utf8", callback);
```

***

要同步追加数据，请使用 `fs.appendFileSync`：

```ts theme={"theme":{"light":"github-light","dark":"dracula"}}
import { appendFileSync } from "node:fs";

appendFileSync("message.txt", "data to append", "utf8");
```

***

更多信息请参见 [Node.js 文档](https://nodejs.org/api/fs.html#fspromisesappendfilepath-data-options)。
