Last Updated : Jul 1, 2023
Now move to the reading the text file line by line.
It helps the application run more efficiently while saving resources. It enables us to search for the necessary data, and once the pertinent data is located, we may halt the search process and reduce unauthorized memory usage.
Any website that interacts with its users must be able to read files through a browser.
For instance, the file can be accessed and read line by line using the "readline" module. Additionally, people can access the file through local computers or internet.
Using the Readline Module and Line-Reader Module, we will reach the goal.
Method 1: Using Readline Module:
The Readline module, a native JS module, was created specifically for reading material line by line from any readable stream. From the command line, data can be read using it.
Method 2: Using the FileReader() Method:
- Firstly, we create the text file which is used in js code.
- In the first line of js code we print the statement Welcome to Talkerscode with the help of console.log.
- Then with the help of const keyword we use the require(“readline”) is employed to read a data stream from a file.
- Then we use the readline it is a readline.createInterface provides an interface for the readline module to read the content of a file.
- Then we pass the value “text” is passed to a callback.
- After that we print the text which is in our text file using console.log.
The local system's files can have their contents read using the FileReader() method. Additionally, the material is read using the readline module.
The file's source is necessary for both of these techniques.