如何在nodejs中拆分长字符串

时间:2016-08-28 11:41:01

标签: arrays node.js split

var content =  fs.readFileSync(txtPath, 'utf8');
var lines = content.split('\n');

当执行第二行时,它会崩溃

events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: write EPIPE
    at exports._errnoException (util.js:1007:11)
    at WriteWrap.afterWrite (net.js:793:14)

内容大约800k字节,它包含4000行 我能怎么做? 感谢

2 个答案:

答案 0 :(得分:0)

readline.createInterface({ input: fs.createReadStream(txtPath), terminal: false }).on('line', function(line) { console.log('Line: ' + line); }).on('close', function(){ console.log('finish') }); 这种方式可以运行,我想要更好的方式〜请

答案 1 :(得分:0)

尝试使用现有的节点模块,例如https://www.npmjs.com/package/line-by-line