nodejs(四)file System模块 解决Cross device link错误 EXDEV
1 var fs = require('fs'); 2 3 /*cross device link 4 fs.rename('c:\\err.LOG','d:\\err.LOG',function(err){ 5 console.log(err.code); 6 }); 7 */ 8 9 move('c:\\err.LOG','d:\\err.LOG',function(err){ 10 if(err) throw err; 11 }); 12 13 14 function move(oldpath,newpath,callback){ 15 fs.rename(oldpath,newpath,function(err){ 16 if(err){ 17 if(err.code === 'EXDEV'){ 18 copy(); 19 }else{ 20 callback(err); 21 } 22 }else{ 23 callback(); 24 } 25 }); 26 27 function copy(){ 28 var readStream = fs.createReadStream(oldpath); 29 var writeStream = fs.createWriteStream(newpath); 30 readStream.on('error',callback); 31 writeStream.on('error',callback); 32 readStream.on('close',function(){ 33 fs.unlink(oldpath,callback); 34 }); 35 } 36 }