Javascript | Program to read text File

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

  • Instead of converting buffer into text using tostring function, directly get the data into text format also.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

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

Check out this Author's contributed articles.

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.