![]() |
VOOZH | about |
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 that handles all the reading operations called fs (File-System). It is basically a JavaScript program (fs.js) where a function for reading operations is written. Import fs-module in the program and use functions to read text from the files in the system.
How to import a library in JavaScript. Read from here: JavaScript | Importing and Exporting Modules.
readFile( Path, Options, Callback);Example: Suppose there is a file with the name Input.txt in the same folder as the JavaScript program.
Example: In this example, we are creating Instead of converting buffer into text using the tostring function, directly get the data into text format also.
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.