Skip to content

Instantly share code, notes, and snippets.

@fearphage
Created December 11, 2010 00:16
Show Gist options
  • Select an option

  • Save fearphage/737019 to your computer and use it in GitHub Desktop.

Select an option

Save fearphage/737019 to your computer and use it in GitHub Desktop.
File copy with streaming API
// Node.js example from the forthcoming 6th edition of JavaScript: The Definitive Guide
// Written by David Flanagan.
// File copy with streaming API.
// Pass a callback if you want to know when it is done
// Note that no error handling is even attempted here...
function fileCopy(filename1, filename2, done) {
var input = fs.createReadStream(filename1); // Input stream
var output = fs.createWriteStream(filename2); // Output stream
input.on("data", function(d) { output.write(d); }); // Copy in to out
input.on("error", function(err) { throw err; }); // Raise errors
input.on("end", function() { // When input ends
output.end(); // close output
if (done) done(); // And notify callback
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment