Last active
December 17, 2018 05:54
-
-
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
This file contains hidden or 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
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