Skip to content

Instantly share code, notes, and snippets.

@SiZapPaaiGwat
Forked from outmost/gist:6388914
Created December 30, 2016 06:18
Show Gist options
  • Save SiZapPaaiGwat/c374919e11b2a5ca4944a8f1be97ccc0 to your computer and use it in GitHub Desktop.
Save SiZapPaaiGwat/c374919e11b2a5ca4944a8f1be97ccc0 to your computer and use it in GitHub Desktop.
Read and Write large files using NodeJS
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