达到我理解的极限。我有一个大文本文件,我需要分成每行200行(使用mime.types练习)。我使用readline
,但它似乎忽略了我的pause / resume
语句。我错过了什么:
// Splits a given file into smaller subfiles by line number
var infileName = 'mime.types';
var fileCount = 1;
var count = 0;
var fs = require('fs');
var outfileName = infileName + '.' + fileCount;
var inStream = fs.createReadStream(infileName);
var outStream = fs.createWriteStream(outfileName);
var lineReader = require('readline').createInterface({
input: inStream
});
lineReader.on('line', function(line) {
count++;
lineReader.pause();
outStream.write(line + '\n');
if (count >= 200) {
fileCount++;
console.log('file ', outfileName, count);
outStream.close();
outfileName = infileName + '.' + fileCount;
outStream = fs.createWriteStream(outfileName);
count = 0;
}
lineReader.resume();
});
lineReader.on('close', function() {
if (count > 0) {
console.log('Final close:', outfileName, count);
}
inStream.close();
outStream.close();
console.log('Done');
});
我还尝试将流的更改放入写操作的回调中:
lineReader.on('line', function(line) {
count++;
lineReader.pause();
outStream.write(line + '\n', function() {
if (count >= 200) {
fileCount++;
console.log('file ', outfileName, count);
outStream.close();
outfileName = infileName + '.' + fileCount;
outStream = fs.createWriteStream(outfileName);
count = 0;
}
lineReader.resume();
});
});
两种情况下结果相同:
node split
file mime.types.1 1588
Done
理想情况下,我想要一个不依赖额外npm的解决方案
答案 0 :(得分:3)
我摆脱了暂停/恢复。它们似乎不受readline流的支持。我所做的就是每200行提供一个新的可写流。
// Splits a given file into smaller subfiles by line number
var infileName = 'mime.types';
var fileCount = 1;
var count = 0;
var fs = require('fs');
var outStream;
var outfileName = infileName + '.' + fileCount;
newWriteStream();
var inStream = fs.createReadStream(infileName);
var lineReader = require('readline').createInterface({
input: inStream
});
function newWriteStream(){
outfileName = infileName + '.' + fileCount;
outStream = fs.createWriteStream(outfileName);
count = 0;
}
lineReader.on('line', function(line) {
count++;
outStream.write(line + '\n');
if (count >= 200) {
fileCount++;
console.log('file ', outfileName, count);
outStream.end();
newWriteStream();
}
});
lineReader.on('close', function() {
if (count > 0) {
console.log('Final close:', outfileName, count);
}
inStream.close();
outStream.end();
console.log('Done');
});