![]() |
VOOZH | about |
The fs.appendFile() method in Node.js is used to add data to the end of a file without deleting its existing content. It works asynchronously, which means it doesn't block other operations. If the file doesn’t exist, it is automatically created. This method is useful for tasks like logging or saving updates.
Syntax:
fs.appendFile( path, data[, options], callback )This method accepts four parameters, as described below:
path (String | Buffer | URL | Number):data (String | Buffer):options (Optional) (String | Object):callback (Function):Below examples illustrate the fs.appendFile() method in Node.js:
Example 1: This example shows the appending of the given text to a file.
Output:
File Contents of file before append: Hello
File Contents of file after append: HelloWorldExample 2: This example shows the usage of the optional parameters to change the file encoding, mode, and flag of the operation.
Output:
File Contents of file before append: This is a test file
File Contents of file after append: This is a test file - GeeksForGeeksWe have a Cheat Sheet on Node fs modules where we covered all the fs methods to check those please go through Node File System Complete Reference article.