Node.js filehandle.writeFile() Method from class: FileHandle
The filehandle.writeFile() method is used to defined in the File System module of Node.js. The File System module is basically to interact with the hard disk of the user’s computer. The fs.writeFile() method asynchronously writes data to a file, replacing the file if it already exists.
Parameter: This method accepts two parameters as mentioned above and described below:
- data: It is a String, Buffer or Uint8Array instance. It is the data which will write to the file.
- options: It is an optional parameter that affects the output in someway accordingly we provide it to the function call or not.
- encoding: It is a string that specifies the encoding technique, default value is ‘utf8’.
Example 1: This example explains how to write operation done to the file that already exists.
Example 2: This example explains how to write operation done to the file that not exist earlier but created at run time.
Directory structure before running the program:
Directory structure after running the program: