Node.js examples for File:File Copy
Copy file
var path = require('path'); var fs = require('fs'); var crypto = require('crypto'); /*copy file*//* w w w. j a v a 2 s .com*/ exports.copyFileSync = function(fromPath,toPath){ if(fs.existsSync(toPath)){ fs.unlinkSync(toPath); }else{ exports.mkdirSync(path.dirname(toPath)); } var BUF_LENGTH = 64*1024 var buff = new Buffer(BUF_LENGTH) var fdr = fs.openSync(fromPath, 'r'); var fdw = fs.openSync(toPath, 'w'); var bytesRead = 1; var pos = 0; while (bytesRead > 0){ bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos); fs.writeSync(fdw,buff,0,bytesRead); pos += bytesRead; } fs.closeSync(fdr); fs.closeSync(fdw); }