Pre-requisite: How to import a library in JavaScript. Read from here: JavaScript | Importing and Exporting Modules.
Given a text file, write a JavaScript program to extract the contents of that file. There is a built-in Module or in-built library in NodeJs which handles all the reading operations called fs (File-System). It is basically a JavaScript program (fs.js) where function for reading operations is written. Import fs-module in the program and use functions to read text from the files in the system.
Used Function: The readFile() functions is used for reading operation.
Syntax:
readFile( Path, Options, Callback)
Parameters: This method accepts three parameters as mentioned above and described below:
- path: It takes in relative path from the program to the text File. If both file and program is in the same folder, the simply give the file name of the text file.
- Options: It is an optional parameter which specify the data is to be read from the file. If nothing is passed, then default raw buffer is returned.
- Callback Function: It is the callback function which has further two arguments (err, data). If the operation fails to extract the data, err shows what is the fault, else data argument will contain the data from the file.
Suppose there is file with name Input.txt in the same folder as the JavaScript program.
- Input.txt file: This is some data inside file Input.txt.
- Script.js:
<script>// Requiring fs module in which// readFile function is defined.const fs = require('fs')fs.readFile('Input.txt', (err, data) => {if(err)throwerr;console.log(data.toString());})</script>chevron_rightfilter_none -
Instead of converting buffer into text using tostring function, directly get the data into text format also.
<script>// Requiring fs module in which// readFile function is defined.const fs = require('fs')// Reading data in utf-8 format// which is a type of character set.// Instead of 'utf-8' it can be// other character set also like 'ascii'fs.readFile('Input.txt','utf-8', (err, data) => {if(err)throwerr;// Converting Raw Buffer to text// data using tostring function.console.log(data);})</script>chevron_rightfilter_none
Output:
This is some data inside file Input.txt.
Note: To run the script first make both files in the same folder and then run script.js using NodeJs interpreter in terminal.
Recommended Posts:
- How to read a local text file using JavaScript?
- Convert Text and Text File to PDF using Python
- How to include a JavaScript file in another JavaScript file ?
- JavaScript | Program to write data in a text File
- Read JSON file using Python
- How to read and write JSON file using Node.js ?
- How to read a file line by line using node.js ?
- How to select all text in HTML text input when clicked using JavaScript?
- How to load the contents of a text file into a JavaScript variable?
- JavaScript | WebAPI | File | File.type Property
- JavaScript | WebAPI | File | File.size Property
- JavaScript | WebAPI | File | File.name Property
- Remove the Chrome's "No file chosen option" from a file input using JavaScript
- Python program to reverse the content of a file and store it in another file
- How to read CSS rule values with JavaScript?
- JavaScript TypeError - "X" is read-only
- Transforming a Plain Text message to Cipher Text
- Text Animation with changing the color of the text using HTML & CSS
- How to create long shadow of text without using text-shadow in HTML and CSS ?
- Convert Text file to JSON in Python
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.


