Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created December 12, 2011 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhurliman/1469062 to your computer and use it in GitHub Desktop.
Save jhurliman/1469062 to your computer and use it in GitHub Desktop.
Stream from one file to another one line at a time
var fs = require('fs');
var input = fs.createReadStream('input.txt');
var output = fs.createWriteStream('output.txt');
var buffer = '';
input.on('data', function(data) {
buffer += data;
var index = buffer.indexOf('\n');
while (index > -1) {
var line = buffer.substr(0, index);
buffer = buffer.substr(index + 1);
parseLine(line);
index = buffer.indexOf('\n');
}
// Show progress on the console
process.stdout.write('.');
});
input.on('end', function() {
console.log('Finished reading input');
output.end(); // Terminate the stream
output.destroySoon(); // Close the file when the write queue is drained
});
output.on('drain', function() {
// Output buffer is empty again, continue reading
input.resume();
});
function parseLine(line) {
// Apply a simple upper-case transformation. If the kernel buffer is full,
// stop reading from the input file until the output.drain event is called
if (!output.write(line.toUpperCase() + '\n', 'ascii'))
input.pause();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment