-
-
Save SiZapPaaiGwat/c374919e11b2a5ca4944a8f1be97ccc0 to your computer and use it in GitHub Desktop.
Read and Write large files using NodeJS
This file contains 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'); | |
var readline = require('readline'); | |
var stream = require('stream'); | |
var inputfile = "pathtofile.txt"; | |
var outputfile = "pathtooutputfile.json"; | |
var instream = fs.createReadStream(inputfile); | |
var outstream = fs.createWriteStream(outputfile, {'flags': 'a'}); | |
outstream.readable = true; | |
outstream.writable = true; | |
var rl = readline.createInterface(instream, outstream); | |
rl.on("line", function(line){this.emit("pause", line);}); | |
rl.on("pause", function(line) { | |
console.log("pause"); | |
console.log("doing some work"); | |
// do some work here | |
var data = JSON.stringify("yourdata":line); | |
console.log(data); | |
outstream.write(data + '\n'); | |
this.emit("resume"); | |
}); | |
rl.on("resume", function() { | |
console.log("resume"); | |
}); | |
rl.on("close", function() { | |
console.log("close"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment