Javascript 如何使用node.js将大数组写入.txt文件?
我想,显而易见的解决方案是使用fs.writeFile 但问题的答案表明我应该使用流技术 我目前正在尝试以下代码,以便通过将文本文件中的一行转换为数组来删除该行:Javascript 如何使用node.js将大数组写入.txt文件?,javascript,arrays,node.js,text,file-io,Javascript,Arrays,Node.js,Text,File Io,我想,显而易见的解决方案是使用fs.writeFile 但问题的答案表明我应该使用流技术 我目前正在尝试以下代码,以便通过将文本文件中的一行转换为数组来删除该行: var index = randomIntFromInterval(1, unfinished_searches.length-1); // remove index unfinished_searches.splice(index, 1); fs.truncate('unfinished_searches.txt', 0, fun
var index = randomIntFromInterval(1, unfinished_searches.length-1); // remove index
unfinished_searches.splice(index, 1);
fs.truncate('unfinished_searches.txt', 0, function()
{
var file = fs.createWriteStream('unfinished_searches.txt');
file.on('error', function(err) { /* error handling */ });
unfinished_searches.forEach(function(v) { file.write(v.join(', ') + '\n'); });
file.end();
})
返回以下错误:
TypeError: undefined is not a function
在处,将加入此行:
unfinished_searches.forEach(function(v) { file.write(v.join(', ') + '\n'); });
如果要从大文本文件中删除行,请考虑使用<代码>管道< /代码>。只需创建
读取流
,将其传输到“检查函数”,然后将其传输到写入流
这样,您就不必将整个文件加载到内存中。内存中一次只会有少量数据。当然,您不能将其写入同一个文件。但在完成操作后,您可以重命名此文件并删除旧文件
编辑:
这篇文章对你很有用:什么是v
?在控制台中打印并检查