-1
我編寫了下面的代碼,將字符串「Write this to the file」寫入文件「my_file」。在執行時,我從控制檯的fs.write的回調中獲得「FileWrite Completed Success」。但是我在文件中看不到任何東西。爲什麼下面的代碼沒有將字符串「寫入文件」寫入文件「my_file」
var fs = require('fs');
function openFileAndAppend(writeBuffer){
fs.open('./my_file','a',function opened(err,fd){
if(err){
console.log(err);
fs.close(fd,function(err){
if(err){
console.log(err);
console.log("Problem in closing the file upon the Open;");
}
});
}
var bufferOffset = 0;
var bufferLength = writeBuffer.lenght;
var filePosition = null;
fs.write(fd,writeBuffer,bufferOffset,bufferLength,filePosition,function(err){
if(err){
console.log(err);
}
console.log("FileWrite Completed Successfully");
fs.close(fd,function(err){
console.log("Closing the file");
if(err){
console.log(err);
console.log("Problem in closing the file after the write");
}
});
});
});
}
openFileAndAppend(new Buffer('Write this to the file'));
請幫助查找錯誤。
在此先感謝!