Created
December 11, 2010 00:16
-
-
Save fearphage/737019 to your computer and use it in GitHub Desktop.
File copy with streaming API
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
| // 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