Javascript | Program to read text File - GeeksforGeeks

12 Oct, 2018
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) throw err;      console.log(data.toString());})</script>
<script>// Requiring fs module in which // readFile function is defined.const fs = require('fs')  fs.readFile('Input.txt', (err, data) => {    if (err) throw err;      console.log(data.toString());})</script>
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) throw err;      // Converting Raw Buffer to text    // data using tostring function.    console.log(data);})</script>
<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) throw err;      // Converting Raw Buffer to text    // data using tostring function.    console.log(data);})</script>
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.
File Handling
Technical Scripter 2018
JavaScript
Technical Scripter
Web Technologies
File Handling
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?