← Back to Node.js Course | Chapter 5: File System | Lesson 3 of 7

Append करना

appendFile वहाँ जो है उसे मिटाए बिना एक file के अंत में text add करता है।

In this page:

  1. Appending
Syntax
javascript
fs.appendFileSync('path', data);
fs.appendFile('path', data, (err) => {
  // handle err
});

Appending

appendFile और appendFileSync ज़रूरत पड़ने पर file बनाते हैं और अंत में data add करते हैं, जो logs के लिए suit करता है। हर call file open और close करता है, इसलिए heavy logging के लिए a flag से एक write stream open रखें।

Note: newline खुद add करें; append नहीं करता।

उदाहरण: Appending

javascript
const fs = require("fs");
fs.writeFileSync("log.txt", "");
for (const msg of ["start", "work", "end"]) fs.appendFileSync("log.txt", msg + "\n");
console.log(fs.readFileSync("log.txt", "utf8").trim().split("\n"));

// Output:
// [ 'start', 'work', 'end' ]

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. entries के बीच newline भूल जाना
  2. हर बार open और close के साथ tight loops में append करना
  3. यह उम्मीद करना कि append शुरुआत में insert करेगा
चैप्टर सारांश
  • appendFile अंत में add करता है
  • यह missing files बनाता है
  • आप newline supply करते हैं
  • Streams heavy logging के लिए suit करती हैं
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.