-
-
Save gaurang847/ab7a258fff02398450c701b4ca15575a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs"); | |
let data = ''; | |
// Create a readable stream | |
let readerStream = fs.createReadStream('input.txt', {encoding: 'UTF8'}); | |
// Handle stream events --> data, end, and error | |
// Note that file read/write involves I/O operations. | |
// And it is thus, an asynchronous task. | |
// 'data' event signifies that a chunk of data is read from the file | |
readerStream.on('data', function(chunk) { | |
data += chunk; | |
}); | |
// 'end' event signifies that the end of file has been reached | |
readerStream.on('end',function() { | |
console.log(data); | |
}); | |
// 'error' event signifies that some error occured while reading file | |
readerStream.on('error', function(err) { | |
console.log(err.stack); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment