Skip to content

Instantly share code, notes, and snippets.

@pbojinov
Last active December 17, 2018 05:54
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 pbojinov/6267730e880d26afe6a5eb4206417536 to your computer and use it in GitHub Desktop.
Save pbojinov/6267730e880d26afe6a5eb4206417536 to your computer and use it in GitHub Desktop.
streaming read from a file line by line then write to another output file
var fs = require('fs');
const INPUT = 'input.txt';
const OUTPUT = `output_${INPUT}`;
var lineReader = require('readline').createInterface({
input: require('fs').createReadStream(INPUT)
});
var wstream = fs.createWriteStream(OUTPUT);
lineReader.on('line', function (line) {
wstream.write(line + '\n');
});
lineReader.on('close', (input) => {
console.log('done');
wstream.end();
});
wstream.on('finish', function () {
console.log('file has been written');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment