我编写了下面的代码,将字符串“将其写入文件”写入文件“my_file”。在执行时,我在控制台上获得“FileWrite Completed Successfully” - 来自fs.write的回调。但我在文件中看不到任何内容。
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'));
请帮助查找错误。
提前致谢!!
答案 0 :(得分:0)
发布此问题后我发现了错误:
var bufferLength = writeBuffer中有一个拼写错误。 lenght;
谢谢