> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/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={null}
import { appendFile } from "node:fs/promises";

await appendFile("message.txt", "要追加的数据");
```

***

使用非 `Promise` API：

```ts theme={null}
import { appendFile } from "node:fs";

appendFile("message.txt", "要追加的数据", err => {
  if (err) throw err;
  console.log('"要追加的数据" 已追加到文件！');
});
```

***

指定内容的编码：

```js theme={null}
import { appendFile } from "node:fs";

appendFile("message.txt", "要追加的数据", "utf8", callback);
```

***

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

```ts theme={null}
import { appendFileSync } from "node:fs";

appendFileSync("message.txt", "要追加的数据", "utf8");
```

***

参见 [Node.js 文档](https://nodejs.org/api/fs.html#fspromisesappendfilepath-data-options) 了解 `fs.appendFile`。
