Skip to content

Instantly share code, notes, and snippets.

@AttilaGal
Created December 2, 2019 15:11
Show Gist options
  • Save AttilaGal/7465abdcd5806aaf105a1182745bf1c9 to your computer and use it in GitHub Desktop.
Save AttilaGal/7465abdcd5806aaf105a1182745bf1c9 to your computer and use it in GitHub Desktop.
// Read line by line from a file.
const fs = require('fs');
const readline = require('readline');
const stream = require('stream')
function processFile(inputFile) {
const instream = fs.createReadStream(inputFile);
const outstream = new (stream)();
const rl = readline.createInterface(instream, outstream);
rl.on('line', function (line) {
console.log(line);
});
rl.on('close', function (line) {
console.log(line);
console.log('done reading file.');
});
}
processFile('/path/to/a/input/file.txt');
//////////////////////////////////////////////////////////////////////
// Read the entire file in utf8 format.
fs.readFile('input.txt', 'utf8', function (err, data) {
if (err) throw err;
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment