Node.js | fs.appendFileSync() Function

The fs.appendFileSync() method is used to synchronously append the given data to a file. A new file is created if it does not exist. The optional options parameter can be used to modify the behavior of the operation.

Syntax:

fs.appendFileSync( path, data, options])

Parameters: This method accepts three parameters as mentioned above and described below:

  • path: It is a String, Buffer, URL or number that denotes the source filename or file descriptor that will be appended.
  • data: It is a String or Buffer that denotes the data that has to be appended.
  • options: It is an string or an object that can be used to specify optional parameters that will affect the output. It has three optional parameters:
    • encoding: It is a string which specifies the encoding of the file. The default value is ‘utf8’.
    • mode: It is an integer which specifies the file mode. The default value is ‘0o666’.
    • flag: It is a string which specifies the flag used while appending to the file. The default value is ‘a’.

Below examples illustrate the fs.appendFileSync() method in Node.js:

Example 1: This example shows the appending of the given text to a file.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.appendFileSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Get the file contents before the append operation
console.log("\nFile Contents of file before append:",
  fs.readFileSync("example_file.txt", "utf8"));
  
fs.appendFileSync("example_file.txt", " - Geeks For Geeks");
  
// Get the file contents after the append operation
console.log("\nFile Contents of file after append:",
       fs.readFileSync("example_file.txt", "utf8"));

chevron_right


Output:

File Contents of file before append: Hello

File Contents of file after append: Hello - Geeks For Geeks

Example 2: This example shows the usage of the optional parameters to change the file encoding and flag of the operation. The “w” flag replaces the contents of the file instead of appending to it.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.appendFileSync() method
  
// Import the filesystem module
const fs = require('fs');
  
// Get the file contents before the append operation
console.log("\nFile Contents of file before append:",
  fs.readFileSync("example_file.txt", "utf8"));
  
// Append to the file using optional parameters
fs.appendFileSync("example_file.txt",
  "This is the appended text",
  { encoding: "utf8", flag: "w" }
);
  
// Get the file contents after the append operation
console.log("\nFile Contents of file after append:",
       fs.readFileSync("example_file.txt", "utf8"));

chevron_right


Output:

File Contents of file before append: This is a test file

File Contents of file after append: This is the appended text

Reference: https://nodejs.org/api/fs.html#fs_fs_appendfilesync_path_data_options




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.