node.js fs.writeFile不完全覆盖文件

我有一个长度为X的文件,它被长度为XY的string覆盖。 问题是,该文件仍然保留XY以上的信息,以便与第一个更长的文件一样长。 所以这里是我的testing结果是给我适合:

文件开始为:

{ "sOption1": "String", "nOption2": 23.5, "sOption3": "String", "bOption3B": true, "anOption4": [ 5, 6, 7 ], "sNewString": "FruitSalad", "bNewBoolean": false, "iNewNumber": 14.2, "anNewArray": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], "oNewObject": { "bToBeOrNotToBe": true, "sFinishQuote": "That is the question" } } 

将正在写入的数据更改为以下内容:

 { "sOption1": "String", "nOption2": 23.5, "sOption3": "String", "bOption3B": true, "anOption4": [ 5, 6, 7 ], "sNewString": "YummyYummy", "bNewBoolean": true, "iNewNumber": 2.14, "anNewArray": [ 10, 9 ], "oNewObject": { "bToBeOrNotToBe": false, "sNewQuote": "To die, to sleep, no more" } } 

在此之后,该文件现在是:

 { "sOption1": "String", "nOption2": 23.5, "sOption3": "String", "bOption3B": true, "anOption4": [ 5, 6, 7 ], "sNewString": "YummyYummy", "bNewBoolean": true, "iNewNumber": 2.14, "anNewArray": [ 10, 9 ], "oNewObject": { "bToBeOrNotToBe": false, "sNewQuote": "To die, to sleep, no more" } } "bToBeOrNotToBe": true, "sFinishQuote": "That is the question" } }} 

查看对象末尾的垃圾? 这是从以前的文件遗留下来,即使我用下面的代码写出来:

 DeviceConfiguration.prototype.SetPersistentUserOption = function(sNewOptionName, NewOption) { var sNewFile = ""; var fs = require('fs'); //if one of the primitive types, it's simple, just add it to object if(typeof(NewOption) == "string" || typeof(NewOption) == "number" || typeof(NewOption) == "boolean") { this.oPersistentUserOptions[sNewOptionName] = NewOption; } else if(NewOption instanceof Array) { //blank out array if it was there already this.oPersistentUserOptions[sNewOptionName] = []; //now go back and copy each element over one at a time for(var nIndex = 0; nIndex < NewOption.length; nIndex++) { this.oPersistentUserOptions[sNewOptionName][nIndex] = NewOption[nIndex]; } } else if(NewOption instanceof Object) { //blank out object if it was there already this.oPersistentUserOptions[sNewOptionName] = {}; //now go back and copy each element over one at a time for(Member in NewOption) { this.oPersistentUserOptions[sNewOptionName][Member] = NewOption[Member]; } } //stringify the object, and make it pretty with options null, 4 sNewFile = JSON.stringify(this.oPersistentUserOptions, null, 4); //write to the file, parameter is immediately in object memory though fs.writeFile(PERSISTENT_USER_SELECTED_OPTIONS_FILENAME, sNewFile, function(err){console.log(err);}); //fs.writeFileSync(PERSISTENT_USER_SELECTED_OPTIONS_FILENAME, sNewFile); console.log(sNewFile.length); console.log(sNewFile); }; 

我已经检查,以确保sNewFilevariables是正确的长度,它是。 在后续写入磁盘之间,我也暂停了6秒钟,所以我看不出这是一个时间问题。

如果我使用writeFileSync,问题就会消失,但我实在没有办法select为这个应用程序进行同步写入,因为我的计时很关键,而且不想减慢写入磁盘的速度。

我在node.js 0.8.21,但它看起来不像接口已经改变之间的fs和最新版本之间的任何。

其他人打这样的事情? 这是给我适合。 。 。

我刚刚在0.8.21(linux)上testing了这个,并且按照预期工作。

 var fs = require('fs') var str1 = "aaaaaaaaaa" var str2 = "bbbbbb" var str3 = "bbbbbbaaaa" fs.writeFile('test',str1,function(){ fs.writeFile('test',str2,function(){ fs.readFile('test','utf8',function(err,buff){ console.log(buff === str2) console.log(buff === str3) }) }) }) output > node test.js true false