[英]node.js fs.writeFile Not Completely Overwriting File
我有一個長度為X的文件,它被一個長度為XY的字符串覆蓋。 問題是,文件仍然保留過去XY的信息,因此它與第一個較長的文件一樣長。 所以這是我的測試輸出,讓我適合:
文件開頭為:
{
"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);
};
我已經檢查過以確保sNewFile變量的長度是正確的,它是。 在后續寫入磁盤之間我也暫停了6秒,所以我看不出這可能是一個時間問題。
如果我使用writeFileSync,問題就會消失,但我真的沒有為這個應用程序做同步寫操作的選項,因為我的計時關鍵並且不想放慢速度來寫入磁盤。
我在node.js 0.8.21上,但看起來接口沒有更改任何fs和最新版本之間的fs。
有人打過這樣的東西嗎? 這給了我適合。 。 。
我剛剛在0.8.21(linux)上測試了這個,這可以按預期工作。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.