您的位置:首页 > Web前端 > Node.js

nodejs(四)file System模块 解决Cross device link错误 EXDEV

2013-08-24 00:07 232 查看
var fs = require('fs');

/*cross device link
fs.rename('c:\\err.LOG','d:\\err.LOG',function(err){
console.log(err.code);
});
*/

move('c:\\err.LOG','d:\\err.LOG',function(err){
if(err) throw err;
});

function move(oldpath,newpath,callback){
fs.rename(oldpath,newpath,function(err){
if(err){
if(err.code === 'EXDEV'){
copy();
}else{
callback(err);
}
}else{
callback();
}
});

function copy(){
var readStream = fs.createReadStream(oldpath);
var writeStream = fs.createWriteStream(newpath);
readStream.on('error',callback);
writeStream.on('error',callback);
readStream.on('close',function(){
fs.unlink(oldpath,callback);
});
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: