Nodejs - fs.appendFileSync 抛出错误 EBUSY
Nodejs - fs.appendFileSync throws Error EBUSY
我正在尝试将行附加到 nodejs 中的文件。我写了下面的代码。
/* Name : test.js */
/* globals require,__dirname */
var fs = require("fs");
for (var i = 0; i < 100000; i++) {
fs.appendFileSync(__dirname+'/out.txt', "line " + i + "\n");
}
但是当我 运行 这个程序时,我收到以下错误。
$ node test.js
fs.js:584
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: EBUSY: resource busy or locked, open 'd:\<redacted>\out.txt'
at Error (native)
at Object.fs.openSync (fs.js:584:18)
at Object.fs.writeFileSync (fs.js:1224:33)
at Object.fs.appendFileSync (fs.js:1283:6)
at Object.<anonymous> (d:\<redacted>\test.js:3:8)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
我已经搜索过,但没有找到任何原因或解决方案。有人可以解释为什么会出现此错误以及如何解决它吗?
当我尝试以下方法时,它起作用了,但这对于我试图解决的问题来说不太可行。
/* globals require,__dirname */
var fs = require("fs");
var buffer = "";
for (var i = 0; i < 100000; i += 1) {
var line = 'line ' + i + '\n';
buffer += line; // keep the line in buffer
}
fs.appendFileSync(__dirname+'/out.txt', buffer); // now write the buffer
提前致谢。
你的代码对我有用,但它非常慢,完成文件写入需要几分钟(准确地说是 269621.425 毫秒)。您的问题可能是因为在很短的时间内访问次数过多,我想它对我有用,因为我使用的是 SSD。
为了您的问题,streams might be very helpful. It's fast and memory efficient (you don't have to store a giant buffer variable). I don't know what you want to do exactly so here is your example written using streams (I used event-stream
为了方便起见):
var fs = require('fs');
var es = require('event-stream');
console.time('done in');
es.readable(function (count, next) {
for (var i = 0; i < 100000; i++) {
this.emit('data', 'line ' + i + "\n");
}
this.emit('end');
console.timeEnd('done in');
next();
}).pipe(fs.createWriteStream('out.txt'));
done in: 90.561ms
我正在尝试将行附加到 nodejs 中的文件。我写了下面的代码。
/* Name : test.js */
/* globals require,__dirname */
var fs = require("fs");
for (var i = 0; i < 100000; i++) {
fs.appendFileSync(__dirname+'/out.txt', "line " + i + "\n");
}
但是当我 运行 这个程序时,我收到以下错误。
$ node test.js
fs.js:584
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: EBUSY: resource busy or locked, open 'd:\<redacted>\out.txt'
at Error (native)
at Object.fs.openSync (fs.js:584:18)
at Object.fs.writeFileSync (fs.js:1224:33)
at Object.fs.appendFileSync (fs.js:1283:6)
at Object.<anonymous> (d:\<redacted>\test.js:3:8)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
我已经搜索过,但没有找到任何原因或解决方案。有人可以解释为什么会出现此错误以及如何解决它吗?
当我尝试以下方法时,它起作用了,但这对于我试图解决的问题来说不太可行。
/* globals require,__dirname */
var fs = require("fs");
var buffer = "";
for (var i = 0; i < 100000; i += 1) {
var line = 'line ' + i + '\n';
buffer += line; // keep the line in buffer
}
fs.appendFileSync(__dirname+'/out.txt', buffer); // now write the buffer
提前致谢。
你的代码对我有用,但它非常慢,完成文件写入需要几分钟(准确地说是 269621.425 毫秒)。您的问题可能是因为在很短的时间内访问次数过多,我想它对我有用,因为我使用的是 SSD。
为了您的问题,streams might be very helpful. It's fast and memory efficient (you don't have to store a giant buffer variable). I don't know what you want to do exactly so here is your example written using streams (I used event-stream
为了方便起见):
var fs = require('fs');
var es = require('event-stream');
console.time('done in');
es.readable(function (count, next) {
for (var i = 0; i < 100000; i++) {
this.emit('data', 'line ' + i + "\n");
}
this.emit('end');
console.timeEnd('done in');
next();
}).pipe(fs.createWriteStream('out.txt'));
done in: 90.561ms