Concept Flow - Appending to files
Start
Open file in append mode
Write new data at file end
Close file
End
The process opens a file in append mode, adds new data at the end, then closes the file.
import { appendFile } from 'node:fs/promises'; await appendFile('log.txt', 'New entry\n');
| Step | Action | File State Before | File State After | Result |
|---|---|---|---|---|
| 1 | Open 'log.txt' in append mode | log.txt content: 'Line1\nLine2\n' | File ready for appending | File opened successfully |
| 2 | Append 'New entry\n' to file | log.txt content: 'Line1\nLine2\n' | log.txt content: 'Line1\nLine2\nNew entry\n' | Data appended at file end |
| 3 | Close file | File open | File closed | File closed successfully |
| 4 | End | File closed | File closed | Operation complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| fileContent | 'Line1\nLine2\n' | 'Line1\nLine2\n' | 'Line1\nLine2\nNew entry\n' | 'Line1\nLine2\nNew entry\n' | 'Line1\nLine2\nNew entry\n' |
| fileStatus | closed | open | open | closed | closed |
Appending to files in Node.js: - Use fs/promises.appendFile(path, data) - Opens file in append mode - Adds data at file end without overwriting - Creates file if missing - Always close file or use promises for safety