Node.js fs.writeFile()未写入文件
根据文档,Node.js fs.writeFile()未写入文件,node.js,Node.js,根据文档,fs.writeFile的默认行为是截断文件(如果文件已经存在)。对我来说,它似乎并没有这样做,而且它可以通过一个非常简单的测试用例观察到 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 whic
fs.writeFile
的默认行为是截断文件(如果文件已经存在)。对我来说,它似乎并没有这样做,而且它可以通过一个非常简单的测试用例观察到
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
})
})
})
预期产出:
真的
假的
实际产量:
假的
真的
这发生在CentOS上,节点为
v0.10.24
。我举的例子来自另一个SO问题,答案的作者说他的机器给他的代码块与我的机器完全相反 这个问题是Vagrant winnfs插件中的一个已知错误。与此问题相关的文件问题已经存在。值得一提的是,Windows和Ubuntu上的输出与预期一致(真/假)(我使用的是旧版本的Node,不确定这是否重要)。您能否详细说明Node的哪个版本?