Open In App
Related Articles

Javascript | Program to read text File

Improve Article
Improve
Save Article
Save
Like Article
Like

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, then 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: 

javascript




<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. 

javascript




<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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Jan, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials