The ‘fs’ module of Node.js implements the File I/O operation. Methods in the fs module can be synchronous as well as asynchronous. The Asynchronous function has a callback function as the last parameter which indicates the completion of the asynchronous function. Node.js developers prefer asynchronous methods over synchronous methods as asynchronous methods never block a program during its execution, whereas the later does. Blocking the main thread is malpractice in Node.js, thus synchronous functions should only be used for debugging or when no other options are available. The fs.writeFileSync() is a synchronous method. The fs.writeFileSync() creates a new file if the specified file does not exist. Also the ‘readline-sync’ module is used to enable user input at runtime.
fs.writeFileSync( file, data, options )
Parameters: This method accept three parameters as mentioned above and described below:
- file: It is a string, Buffer, URL or file description integer that denotes the path of the file where it has to be written. Using a file descriptor will make the it behave similar to fs.write() method.
- data: It is a string, Buffer, TypedArray or DataView that will be written to the file.
- options: It is an string or object that can be used to specify optional parameters that will affect the output. It has three optional parameter:
- 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 writing to the file. The default value is ‘w’.
Below examples illustrate the fs.writeFileSync() method in Node.js:
File written successfully The written has the following contents: This is a file containing a collection of programming languages. 1. C 2. C++ 3. Python
File written successfully 5 times The written file has the following contents: Movie 0 Movie 1 Movie 2 Movie 3 Movie 4
Example 3: Taking runtime input from users for file name and file data using readline module
Example 4: Taking runtime input from users for file data using readline module using buffer
- Nodejs | Automatic restart NodeJs server with nodemon
- Nodejs console.timeLog() Method
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Introduction to NodeJS
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
- Difference between NodeJS and AngularJS
- Nodejs – Connect Mysql with Node app
- Nodejs | DNS | setServers()
- NodeJS | Building simple REST API in express
- NodeJs - Handling invalid routes
- Nodejs | authentication using Passportjs and passport-local-mongoose
- Nodejs | GM charcoal() Function
- Nodejs | GM blur() Function
- Nodejs | GM sharpen() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Shreyasi_Chakraborty