Node.js fs.writeFile()不包含写入文件

根据文档, fs.writeFile的默认行为是截断文件,如果它已经存在。 它似乎没有为我做这件事,而且用一个非常简单的testing用例就可以观察到它。

 var fs = require('fs') var str1 = "aaaaaaaaaa" // start with this string var str2 = "bbbbbb" // replace it with this string var str3 = "bbbbbbaaaa" // this is the string which appears with an append fs.writeFile('test',str1,function(){ fs.writeFile('test',str2,function(){ fs.readFile('test','utf8',function(err,buff){ console.log(buff === str2) // should be true console.log(buff === str3) // should be false }) }) }) 

预期输出: true false

实际输出: false true

这在CentOS上发生,节点v0.10.24 。 我给出的例子来自另一个SO问题,那个答案的作者指出,他的机器给了他和我的机器相同的代码块。 node.js fs.writeFile不完全覆盖文件 。