aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar nygma <29187259+nygmaaa@users.noreply.github.com> 2023-10-20 08:29:31 +0800
committerGravatar GitHub <noreply@github.com> 2023-10-19 17:29:31 -0700
commite9948f1291723ca61b6953713570841a0f0069d1 (patch)
tree2e61618996aa7aacd1a8fe3280e8756b10b716c4
parent378385ba60900be7bd797923c219ec489101f2f5 (diff)
downloadbun-e9948f1291723ca61b6953713570841a0f0069d1.tar.gz
bun-e9948f1291723ca61b6953713570841a0f0069d1.tar.zst
bun-e9948f1291723ca61b6953713570841a0f0069d1.zip
Add append content to a file guide (#6581)
* Add append content guide Resolve #6559 * Update guide --------- Co-authored-by: Colin McDonnell <colinmcd94@gmail.com>
-rw-r--r--docs/guides/write-file/append.md52
1 files changed, 52 insertions, 0 deletions
diff --git a/docs/guides/write-file/append.md b/docs/guides/write-file/append.md
new file mode 100644
index 000000000..40f534a62
--- /dev/null
+++ b/docs/guides/write-file/append.md
@@ -0,0 +1,52 @@
+---
+name: Append content to a file
+---
+
+Bun implements the `node:fs` module, which includes the `fs.appendFile` and `fs.appendFileSync` functions for appending content to files.
+
+---
+
+You can use `fs.appendFile` to asynchronously append data to a file, creating the file if it does not yet exist. The content can be a string or a `Buffer`.
+
+```ts
+import { appendFile } from "node:fs/promises";
+
+await appendFile("message.txt", "data to append");
+```
+
+---
+
+To use the non-`Promise` API:
+
+```ts
+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!');
+});
+```
+
+---
+
+To specify the encoding of the content:
+
+```js
+import { appendFile } from "node:fs";
+
+appendFile("message.txt", "data to append", "utf8", callback);
+```
+
+---
+
+To append the data synchronously, use `fs.appendFileSync`:
+
+```ts
+import { appendFileSync } from "node:fs";
+
+appendFileSync("message.txt", "data to append", "utf8");
+```
+
+---
+
+See the [Node.js documentation](https://nodejs.org/api/fs.html#fspromisesappendfilepath-data-options) for more information.