
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read and Write a File Using JavaScript
The read and write operations on a file can be done by using specific commands. The module required to perform these operations must be imported first. The required module is 'fs', which is called the File System module in Node.js
Write Operation on a File
After the File System file is imported then, the writeFile() operation is called. The writeFile() method is used to write into the file in JavaScript.
Syntax
writeFile(path, inputData, callBackFunc)
Parameters
The writeFile() function accepts three parameters as mentioned below.
- Path: The first parameter is the path of the file or the name of the file into which the input data is to be written.
- inputData: The second parameter is the input data which contains the data to be written in the file that is opened.
- callBackFunc: The third parameter is the function which is the callback function that takes the error as the parameter and shows the fault if the write operation fails.
If there is a file already, then the contents in the file are deleted and the input which is given by the user will get updated or if the file is not present, then the file with that will be created in the given path and the input information is written into it.
Example Code
Following is an example of the write operation in files in JavaScript.
const fs = require('fs') let fInput = "You are reading the content from Tutorials Point" fs.writeFile('tp.txt', fInput, (err) => { if (err) throw err; else{ console.log("The file is updated with the given data") } })
Output
This will not create a file here. If run the code locally it will work. If you open the file you can observe the written data in it as shown below.
The file is updated with the given data
Read Operation on the File
After the File System module is imported, the reading of the file in JavaScript can be done by using the readFile() function.
Syntax
readFile(path, format, callBackFunc)
Parameters
The readFile() function accepts three parameters as mentioned below.
- Path: The first parameter is the path of the test file from which the contents are to read. If the current location or directory is the same directory where the file which is to be opened and read is located then, only the file name has to be given.
- Format: The second parameter is the optional parameter which is the format of the text file. The format can be ASCII, utf-8 etc.
- CallBackFunc: The third parameter is the callback function which takes the error as the parameter and displays the fault is any raised due to the error.
Example Code
This code will read the contents of the file populated in the previous example and print it.
const fs = require('fs') fs.readFile('tp.txt', (err, inputD) => { if (err) throw err; console.log(inputD.toString()); })
Output
Following is the output of the above example.
You are reading the content from Tutorials Point
Read and Write a File
Following is a combined example of the above of reading and writing files using the fs module on node.js. Let us create a JS file named main.js having the following code.
Example Code
var fs = require("fs"); console.log("Going to write into existing file"); // Open a new file with name input.txt // and write Simply Easy Learning! to it. fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) { console.log("Data written successfully!"); console.log("Let's read newly written data"); // Read the newly written file and print // all of its content on the console fs.readFile('input.txt', function (err, data) { console.log("Asynchronous read: " + data.toString()); }); });
Output
Going to write into existing file Data written successfully! Let's read newly written data Asynchronous read: Simply Easy Learning!