Skip to content

Bun 은 파일에 콘텐츠를 추가하기 위한 fs.appendFilefs.appendFileSync 함수를 포함한 node:fs 모듈을 구현합니다.


fs.appendFile 을 사용하여 파일에 데이터를 비동기적으로 추가할 수 있으며 파일이 아직 존재하지 않으면 생성합니다. 콘텐츠는 문자열 또는 Buffer 일 수 있습니다.

ts
import { appendFile } from "node:fs/promises";

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

Promise API 를 사용하려면:

ts
import { appendFile } from "node:fs";

appendFile("message.txt", "data to append", err => {
  if (err) throw err;
  console.log('"data to append" 가 파일에 추가되었습니다!');
});

콘텐츠의 인코딩을 지정하려면:

js
import { appendFile } from "node:fs";

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

데이터를 동기적으로 추가하려면 fs.appendFileSync 를 사용합니다:

ts
import { appendFileSync } from "node:fs";

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

자세한 내용은 Node.js 문서 를 참조하세요.

Bun by www.bunjs.com.cn 편집