我需要生成大量的字符串值组合,数十亿行,并将它们全部写入一个大文件中的磁盘,而不会阻止进程。
目前,它一直有效,直到文件写入缓冲区被填满,对于少量单词列表,当我尝试使用大量记录时,输出文件仍然为空。
const fs = require('fs');
const words = fs.readFileSync('./1024words.txt').toString().split("\n");
//create array of 1024 words
let fileout
, row = ''
, wordA, wordB, wordC
, totRows = Math.pow(words.length,3)*3 //combination of 3 words for 3 columns of output
, count = 0;
for (let indexA = 0; indexA < words.length; indexA++) {
wordA = words[ indexA ];
fileout = fs.createWriteStream(`${wordA}.csv`); //new output file
for (let indexB = 0; indexB < words.length; indexB++) {
wordB = words[ indexB ];
for (let indexC = 0; indexC < words.length; indexC++) {
wordC = words[ indexC ];
row = `${wordA} ${wordB} ${wordC}\n`;
count++;
process.stdout.write(`${count} of ${totRows} written: ${row}`);
fileout.write(row);
}
}
fileout.end();
fileout.close();
}