Skip to content

Instantly share code, notes, and snippets.

@gaurang847
Created October 27, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaurang847/ab7a258fff02398450c701b4ca15575a to your computer and use it in GitHub Desktop.
Save gaurang847/ab7a258fff02398450c701b4ca15575a to your computer and use it in GitHub Desktop.
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