3 回答
TA贡献1818条经验 获得超7个赞
TA贡献1847条经验 获得超7个赞
appendFile
, appendFile
EMFILE
appendFile
WriteStream
.
appendFile
:
console.log(new Date().toISOString());[...Array(10000)].forEach( function (item,index) { fs.appendFile("append.txt", index+ "\n", function (err) { if (err) console.log(err); });});console.log(new Date().toISOString());
{ Error: EMFILE: too many open files, open 'C:\mypath\append.txt' at Error (native) errno: -4066, code: 'EMFILE', syscall: 'open', path: 'C:\\mypath\\append.txt' }
appendFile
var stream = fs.createWriteStream("append.txt", {flags:'a'});console.log(new Date().toISOString());[...Array(10000)].forEach( function (item,index) { stream.write(index + "\n");});console.log(new Date().toISOString());stream.end();
stream.end()
AutoClose:true
TA贡献1895条经验 获得超3个赞
使用createWriteStream的代码为每次写入创建一个文件描述符。因为它要求节点在写入后立即关闭,因此log.end更好。
var fs = require('fs');
var logStream = fs.createWriteStream('log.txt', {'flags': 'a'});
// use {'flags': 'a'} to append and {'flags': 'w'} to erase and write a new file
logStream.write('Initial line...');
logStream.end('this is the end line');
- 3 回答
- 0 关注
- 479 浏览
添加回答
举报