Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Javascript | Program to read text File

  • Last Updated : 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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!