> ## 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.write()`](/runtime/file-io#writing-files-bun-write) 将字符串写入磁盘上的*绝对路径*。第一个参数是*目标*；第二个参数是要写入的*数据*。

```ts theme={null}
const path = "/path/to/file.txt";
await Bun.write(path, "Lorem ipsum");
```

***

Bun 从当前工作目录解析相对路径。

```ts theme={null}
const path = "./file.txt";
await Bun.write(path, "Lorem ipsum");
```

***

你可以将 `BunFile` 作为目标传递。`Bun.write()` 将数据写入其关联的路径。

```ts theme={null}
const path = Bun.file("./file.txt");
await Bun.write(path, "Lorem ipsum");
```

***

`Bun.write()` 返回写入磁盘的字节数。

```ts theme={null}
const path = "./file.txt";
const bytes = await Bun.write(path, "Lorem ipsum");
// => 11
```

***

参见 [`Bun.write()`](/runtime/file-io#writing-files-bun-write)。
