Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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