Created
August 30, 2013 11:24
-
-
Save outmost/6388914 to your computer and use it in GitHub Desktop.
Read and Write large files using NodeJS
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'); | |
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"); | |
}); |
I dont see using of imported stream
module, any reason to import that module?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks mate